doubi6898 2017-09-30 12: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 13: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

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

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?