opencombatclan Posted September 13, 2009 Share Posted September 13, 2009 I am currently making a certain script for which I need to do some sort of advanced html code replacement. Lets say I got the following html code <tr><td nowrap=\"1\" width=\"33%\"> QWE1 </td> <td nowrap=\"1\" width=\"33%\"> KSJDWS </td> <td nowrap=\"1\" width=\"33%\"> SDKSJWN </td> </tr> PHP only knows the value: QWE1. I need php to replace QWE1, KSJDWS and SDKSJWN with ABC so the code becomes like: <tr><td nowrap=\"1\" width=\"33%\"> ABC </td> <td nowrap=\"1\" width=\"33%\"> ABC </td> <td nowrap=\"1\" width=\"33%\"> ABC </td> </tr> Only the first value (QWE1) is known, the other 2 vars are random. The html pattern is always the same. How can I make php to replace all 3 values with ABC in my pattern? I really dont have a clue... I hope someone can help me out. Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/ Share on other sites More sharing options...
opencombatclan Posted September 15, 2009 Author Share Posted September 15, 2009 any one? Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919066 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 look up preg_replace() Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919069 Share on other sites More sharing options...
thebadbad Posted September 15, 2009 Share Posted September 15, 2009 Is your sample HTML the full string or is it part of a bigger? Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919074 Share on other sites More sharing options...
Garethp Posted September 15, 2009 Share Posted September 15, 2009 $var1 = 'KSJDWS'; $var2 = 'SDKSJWN'; $Search = array( '~QWE1~', '~' . $var1 . '~'. '~' . $var2 . '~' ); $Replace = 'ABC'; $Content = preg_replace($Search, $Replace, $Content); Is that what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919076 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 I dont believe so. He needs to replace a string inside a certain HTML pattern. What you have replaces certain strings with ABC, but in his case, the string can be anything Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919079 Share on other sites More sharing options...
MadTechie Posted September 15, 2009 Share Posted September 15, 2009 It depends on all of the conditions but you could probably use a RegEx, I think you mean you have 3 blocks which you need to replace a value BUT ONLY know the value of the first one. i guess you could try something like this <?php $HTML = "<tr><td nowrap=\"1\" width=\"33%\"> QWE1 </td> <td nowrap=\"1\" width=\"33%\"> KSJDWS </td> <td nowrap=\"1\" width=\"33%\"> SDKSJWN </td> </tr>"; $find = 'QWE1'; $replace = 'ABC'; $HTML = preg_replace('/(<[^<]*?)'.$find.'(.*?\1)[^<]*(.*?\1)[^<]*(<)/sim', '\1'.$replace.'\2'.$replace.'\3'.$replace.'\4', $HTML); echo $HTML; ?> Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919179 Share on other sites More sharing options...
opencombatclan Posted September 16, 2009 Author Share Posted September 16, 2009 Thank you very much MadTechie. This is exactly what I needed. I am trying to learn how to work with preg_replace, and I will try to understand your code. last question: Is it also possible to use a certain script like this if only the third value is known, (in the same html pattern). Or cant preg_replace do such a thing? Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919516 Share on other sites More sharing options...
MadTechie Posted September 16, 2009 Share Posted September 16, 2009 Using the last one would be harder, the second one would be okay the problem is PHP only knows the value: QWE1. the more info you have the easier it becomes, with that in mind i think i am going to have to ask what your doing i assume your scraping a web page, maybe pulling links? either way you can get more info but it depends on what your trying to do Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919544 Share on other sites More sharing options...
opencombatclan Posted September 16, 2009 Author Share Posted September 16, 2009 I use this code to customize a web time table that school provided me. When only the last of the tree values is known, the goal is still the same: all 3 values need to be changed to ABC. Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919681 Share on other sites More sharing options...
MadTechie Posted September 16, 2009 Share Posted September 16, 2009 Well is it always in a table ? you may get away with something like this <?php $HTML = "<tr><td nowrap=\"1\" width=\"33%\"> QWE1 </td> <td nowrap=\"1\" width=\"33%\"> KSJDWS </td> <td nowrap=\"1\" width=\"33%\"> SDKSJWN </td> </tr>"; $find = "SDKSJWN"; $replace = "ABC"; if (preg_match('%<tr>.*?(<[^>]*?>)[^>]*?'.$find.'[^<]*(<[^>]*>).*?</tr>%sim', $HTML, $found)) { $start= $found[1]; $end= $found[2]; $HTML = preg_replace('#('.$start.').*?('.$end.')#sim', '\1'.$replace.'\2', $HTML); } echo $HTML; ?> Quote Link to comment https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.