[ create a new paste ] login | about

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

PHP, pasted on Sep 20:
<?php

     // 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();
?>


Output:
1
<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


Comments: