PHP autolink if not already linked

前端 未结 1 537
梦如初夏
梦如初夏 2021-01-21 16:38

My question is similar to this question:

How to mimic StackOverflow Auto-Link Behavior

However this solution doesn\'t work for mixed content that may already con

相关标签:
1条回答
  • 2021-01-21 16:59

    Load up the string as HTML in ta DOM parser, iterate over the text nodes, and check for a URL. Make sure the text node's parent isn't an <a> tag, so you know that the text you're getting is not already in a link. Now, find all of the URLs, convert them to <a> tags, and replace them in the DOM:

    $doc = new DOMDocument();
    $doc->loadHTML( $str);
    
    $xpath = new DOMXpath($doc);
    foreach( $xpath->query('//text()') as $text) {
        if( !($text->parentNode->tagName == "a")) {
            $frag = $doc->createDocumentFragment();
            $frag->appendXML( preg_replace('#(http://stackoverflow.com/)#', '<a href="$1">$1</a>', $text->data));
            $text->parentNode->replaceChild( $frag, $text);
        }
    }
    

    Note that this relies on a regex to identify URLs, which is a difficult task. I suggest finding one that suits your needs, as it is currently using:

    #(http://stackoverflow.com/)#
    

    However, given this input:

    http://stackoverflow.com/ is a wonderful URL.
    
    <a href="http://stackoverflow.com/">Has already been linked.</a>
    
    <a href="http://stackoverflow.com/">http://stackoverflow.com/</a>
    

    It produces this output:

    <p><a href="http://stackoverflow.com/">http://stackoverflow.com/</a> is a wonderful URL. 
    
    <a href="http://stackoverflow.com/">Has already been linked.</a> 
    
    <a href="http://stackoverflow.com/">http://stackoverflow.com/</a></p>
    
    0 讨论(0)
提交回复
热议问题