How to remove a link from content in php?

前端 未结 7 829
悲哀的现实
悲哀的现实 2020-12-24 09:12

How can i remove the link and remain with the text?

text text text. 
相关标签:
7条回答
  • 2020-12-24 09:49

    I suggest you to keep the text in link.

    strip_tags($text, '<br>');
    

    or the hard way:

    preg_replace('#<a.*?>(.*?)</a>#i', '\1', $text)
    

    If you don't need to keep text in the link

    preg_replace('#<a.*?>.*?</a>#i', '', $text)
    
    0 讨论(0)
  • 2020-12-24 09:51

    Try:

    preg_replace('/<a.*?<\/a>/','',"test test testa<br> <a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>");
    
    0 讨论(0)
  • 2020-12-24 09:52

    strip_tags() will strip HTML tags.

    0 讨论(0)
  • 2020-12-24 10:06

    this is my solutions :

    function removeLink($str){
    $regex = '/<a (.*)<\/a>/isU';
    preg_match_all($regex,$str,$result);
    foreach($result[0] as $rs)
    {
        $regex = '/<a (.*)>(.*)<\/a>/isU';
        $text = preg_replace($regex,'$2',$rs);
        $str = str_replace($rs,$text,$str);
    }
    return $str;}
    

    dang tin rao vat

    0 讨论(0)
  • 2020-12-24 10:09

    Try this one. Very simple!

    $content = "text text text. <br><a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>";
    echo preg_replace("/<a[^>]+\>[a-z]+/i", "", $content);
    

    Output:

    text text text. <br>
    
    0 讨论(0)
  • 2020-12-24 10:11

    One more short solution without regexps:

    function remove_links($s){
        while(TRUE){
            @list($pre,$mid) = explode('<a',$s,2);
            @list($mid,$post) = explode('</a>',$mid,2);
            $s = $pre.$post;
            if (is_null($post))return $s;
        }
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题