0

I have a text with 'a' tags there. I have to add some new tags and attributes.

It looks like this:

'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.'

Now I have to get:

'Some test <noindex><a rel="nofollow" href="site">here</a></noindex>.'
'Yet <noindex><a rel="nofollow" href="site2">another</a></noindex> test.'

Any fast ways to do that with php? Thanks.

Max Frai
  • 61,946
  • 78
  • 197
  • 306

4 Answers4

2

Something like this would cover most real world cases:

$text = 'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.';

$regex = '%(<a\s)(.*?</a>)%i';
$replacement = '<noindex>$1rel="nofollow" $2</noindex>';

preg_replace($regex, $replacement, $text);
Jacob Eggers
  • 9,062
  • 2
  • 25
  • 43
1

Bearing in mind that HTML parsing with regular expression is a bad idea (you should use something like DOMDocument instead), this should do it:

$str = 'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.';
echo preg_replace('/<a(.+?)<\/a>/', '<noindex><a$1</a></noindex>', $str);
// Some test <noindex><a href="site">here</a></noindex>. Yet <noindex><a href="site2">another</a></noindex> test.
Alex
  • 9,313
  • 1
  • 39
  • 44
1

Just wanted to give the DOMDocument (docs) version, since the conventional wisdom says "Don't use RegEx on HTML!!". Well, that's a fine thing to say, but then what!? Well, here you go:

    // create a new DOMDocument
    $doc = new DOMDocument();

    // load the string into the DOM
    $doc->loadHTML('Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.');

    // since we are working with HTML fragments here, remove <!DOCTYPE 
    $doc->removeChild($doc->firstChild);            

    // likewise remove <html><body></body></html> 
    $doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);

    //Loop through each <a> tag in the dom and wrap it with <noindex>
    foreach($doc->getElementsByTagName('a') as $link) {
        $parent = $link->parentNode;
        $ni = $doc->createElement('noindex');
        $ni->appendChild($link->cloneNode(true));
        $parent->replaceChild($ni, $link);
    } 

   echo $doc->saveHTML();

Check it out here: http://codepad.org/ANi93sBj

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
0
$string = preg_replace('~<a.*?href=(.*?)>(.*?)</a>~msi', '<noindex><a rel="nofollow" href=$1>$2</a></noindex>', $html);
genesis
  • 50,477
  • 20
  • 96
  • 125