0

I'm stuck as preg_matching is not always that easy as I'm totally not familiar with it.

I'm trying to replace all the

In example:

<a href="www.site1.com">Site1</a> => <a href="http://example.com?u=www.site1.com">Site1</a>

But the <a href can be written in many ways; a HREF or A href or double spaced <A href etc... How can i manage this. Bear in mind, performance is key

I've tried the following with str_replace, but of course that does not cover all the <a href (capital non capitalized versions).

$str = '<a href="www.sitename1.com">sitename1</a><br /><a href="www.sitename2.com">sitename2</a><br /><A HREF="www.sitename3.com">sitename3</a>';

$Replace = str_replace('<a href="', '<a href="https://example.com/&i=1243123&r=', $str);

echo $Replace
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Peter Fox
  • 1,239
  • 2
  • 11
  • 31

2 Answers2

2

Try this (PHP 5.3+):

$link = preg_replace_callback('#<a(.*?)href="(.*?)"(.*?)>#is', function ($match) {
    return sprintf(
        '<a%shref="%s"%s>',
        $match[1],
        'http://example.com?u=' . urlencode($match[2]),
        $match[3]
    );
}, '<a href="www.site1.com">Site1</a>');

echo $link;
Waldson Patricio
  • 1,489
  • 13
  • 17
  • Looks like this one works. Anybody that has comments to this? Why not use this instead of the dom parse if performance en speed are key? – Peter Fox Jan 12 '15 at 20:34
  • 1
    You have to try them both and see which is faster. But I think this way is faster because this will not parse html and generate a traversable structure. Besides that, you will not iterate over the DOM neither manipulate it and regenerate modified HTML from structure. – Waldson Patricio Jan 12 '15 at 20:39
1

The only fully reliable way of doing this is to use a proper HTML parser.

Happily, PHP has one built-in.

You'd first load the HTML with DomDocument's loadHTML function: http://php.net/manual/en/domdocument.loadhtml.php

Then search the parsed tree with XPath and manipulate the A tags: http://php.net/manual/en/domxpath.query.php

ceejayoz
  • 176,543
  • 40
  • 303
  • 368