[ create a new paste ] login | about

Link: http://codepad.org/ANi93sBj    [ raw code | output | fork ]

PHP, pasted on Sep 20:

     // 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 

    // 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');
        $parent->replaceChild($ni, $link);

   echo $doc->saveHTML();

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

Create a new paste based on this one