doubi6898 2017-09-30 04:21
浏览 105
已采纳

Php使用strip_tags忽略<a>标签中的文本

I want to strip tags. When I use

$ta=strip_tags($_REQUEST['textarea'],'<a>');

it returns <a> tags. If I use

$ta=strip_tags($_REQUEST['textarea']);

it includes the interior of the <a href>.

I want only text. For example with this html

$text= '<p>test paragraph.</p>'<a href="index.php">Click link</a>';

I want only test paragraph, but I'm getting test paragraph.Click link

Thanks for your help

  • 写回答

1条回答 默认 最新

  • dongyuandou2521 2017-09-30 05:47
    关注

    If it's only <a href tags you don't like as commented in the comments above this should clear them away and leave you with the rest that can be easily removed with strip_tags().

    $text= '<p>test paragraph.</p><a href="index.php">Click link</a><p>test paragraph.</p><a href="index.php">Click link</a><p>test paragraph.</p>';
    
    $pos = strpos($text, "<a href"); // find first a href
    
    while($pos !== false){ // loop until there is no more a href
        $pos2 = strpos($text, "</a>", $pos)+4; // find the end tag of the a
        $text = substr($text, 0, $pos) . substr($text, $pos2); // remove the tag and link text
        $pos = strpos($text, "<a href"); // find the next. If none is found "false" is returned meaning while ends.
    }
    
    echo strip_tags($text); // strip away other tags.
    

    https://3v4l.org/YtJic

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部