Convert clickable anchor tags to plain text in html document

前端 未结 2 601
名媛妹妹
名媛妹妹 2020-11-28 16:10
相关标签:
2条回答
  • 2020-11-28 16:56

    You can make the match ungreedy using ?. You should also take into account there may be attributes before the href attribute.

    $result = preg_replace('/<a [^>]*?href="(http:\/\/[A-Za-z0-9\\.:\/]+?)">([\\s\\S]*?)<\/a>/',
        '<strong>\\2</strong> [\\1]', $content);
    
    0 讨论(0)
  • 2020-11-28 17:01

    You should be using DOM to parse HTML, not regular expressions...

    Edit: Updated code to do simple regex parsing on the href attribute value.

    Edit #2: Made the loop regressive so it can handle multiple replacements.

    $content = '
    <p><a href="http://www.website.com">This is a text link</a></p>
    <a href="http://sitename.com/#foo">bah</a>
    
    <a href="#foo">I wont change</a>
    
    ';
    
    
     $dom = new DOMDocument();
        $dom->loadHTML($content);
    
        $anchors = $dom->getElementsByTagName('a');
        $len = $anchors->length;
    
        if ( $len > 0 ) {
            $i = $len-1;
            while ( $i > -1 ) {
            $anchor = $anchors->item( $i );
    
            if ( $anchor->hasAttribute('href') ) {
                $href = $anchor->getAttribute('href');
                $regex = '/^http/';
    
                if ( !preg_match ( $regex, $href ) ) { 
                $i--;
                continue;
                }
    
                $text = $anchor->nodeValue;
                $textNode = $dom->createTextNode( $text );
    
                $strong = $dom->createElement('strong');
                $strong->appendChild( $textNode );
    
                $anchor->parentNode->replaceChild( $strong, $anchor );
            }
            $i--;
            }
        }
    
        echo $dom->saveHTML();
        ?>
    
    0 讨论(0)
提交回复
热议问题