0

This is my string:

<ol>
    <li>
        <a rel="nofollow" href="http://127.0.0.1/index.php/something?price=3%2C25"><span class="price">50,00€</span> - <span class="price">75,00€</span></a> (38)
    </li>
    <li>
        <a rel="nofollow" href="http://127.0.0.1/index.php/something?price=4%2C25"><span class="price">75,00€</span> - <span class="price">100,00€</span></a> (11)
    </li>
</ol>

I want to replace

<span class="price">50,00€</span> - <span class="price">75,00€</span>

with "foobar" and

<span class="price">75,00€</span> - <span class="price">100,00€</span>

with "foobar2" for example. The only way to know which line to replace with what, is the price=3 or price=4 part in the URL.

So after the replacement, the string should look like:

<ol>
    <li>
        <a rel="nofollow" href="http://127.0.0.1/index.php/something?price=3%2C25">foobar</a> (38)
    </li>
    <li>
        <a rel="nofollow" href="http://127.0.0.1/index.php/something?price=4%2C25">foobar2</a> (11)
    </li>
</ol>

I tried preg_replace, but it always gets too much of the string. Ideas?

Thanks for helping!

user1856596
  • 7,027
  • 10
  • 41
  • 63

1 Answers1

0

If you are wanting to change the contents, based on some sort of key, I recommend using an array to hold the values.

<?php

$foo_array = array(3 => 'foobar', 4 => 'foobar2');

Now, I'm just adding in the original string so I have something to operate on:

$string = '<ol>
    <li>
        <a rel="nofollow" href="http://127.0.0.1/index.php/something?price=3%2C25"><span class="price">50,00€</span> - <span class="price">75,00€</span></a> (38)
    </li>
    <li>
        <a rel="nofollow" href="http://127.0.0.1/index.php/something?price=4%2C25"><span class="price">75,00€</span> - <span class="price">100,00€</span></a> (11)
    </li>
</ol>';

Then, you can use something like this to do a replace on your string.

$new_string = preg_replace_callback('/(price=(3|4)([A-Z0-9%]+)">)(.*?)(<\/a>)/ms', function ($m) use ($foo_array) {return "$m[1]".$foo_array[$m[2]]."$m[5]";}, $string);

print $new_string;

Alternatively, if you'd like to heed Marc B's advice in the comments, you can use PHP's DOM manipulations to do the work. I am not an expert in this, but I took a stab at it and this seems to output the same as the REGEX solution above (except this adds the DOCTYPE, html and body headers in):

$dom_document = new DOMDocument(); // CREATE A NEW DOCUMENT
$dom_document->loadHTML($string); // LOAD THE STRING INTO THE DOCUMENT
$links = $dom_document->getElementsByTagName('a'); // PULL OUT THE LINKS OUT OF THE DOCUMENT

// LOOP THROUGH EACH LINK
foreach ($links AS $link) {

    // IF WE FIND A 3 OR A 4 AFTER price=, THEN, REPLACE THE TEXT OF 
    // - THE LINK (THE nodeValue) WITH THE ITEM FROM THE ARRAY
    if (preg_match('/price=(3|4)/ms', $link->getAttribute('href'), $m)) {
        $link->nodeValue = $foo_array[$m[1]];
    }

}

$new_string_2 = $dom_document->saveHTML(); // WRITE THE CHANGES TO A STRING

print $new_string_2;
Quixrick
  • 3,190
  • 1
  • 14
  • 17