开发者

regex for a link

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&开发者_如何学Clt;/a></noindex>.'
'Yet <noindex><a rel="nofollow" href="site2">another</a></noindex> test.'

Any fast ways to do that with php? Thanks.


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


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.


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


$string = preg_replace('~<a.*?href=(.*?)>(.*?)</a>~msi', '<noindex><a rel="nofollow" href=$1>$2</a></noindex>', $html);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜