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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.