Jump to content

php advanced replace?


opencombatclan

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/174099-php-advanced-replace/
Share on other sites

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919179
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919516
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919544
Share on other sites

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/174099-php-advanced-replace/#findComment-919693
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.