加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用PHP从字符串中删除html元素

发布时间:2020-12-13 17:08:59 所属栏目:PHP教程 来源:网络整理
导读:我无法解决如何做到这一点,我有一个字符串看起来像这样…… $text = "pThis is some example text This is some example text This is some example text/p pemThis is some example text This is some example text This is some example text/em/p pThis i
我无法解决如何做到这一点,我有一个字符串看起来像这样……

$text = "<p>This is some example text This is some example text This is some example text</p>
             <p><em>This is some example text This is some example text This is some example text</em></p>
             <p>This is some example text This is some example text This is some example text</p>";

我基本上想要使用像preg_repalce和regex这样的东西来删除

<em>This is some example text This is some example text This is some example text</em>

所以我需要编写一些PHP代码来搜索开头< em>并关闭< / em>并删除其间的所有文本

希望有人可以帮忙,
谢谢.

解决方法

如果您对非正则表达式解决方案感兴趣,请注意:

<?php
    $text = "<p>This is some example text This is some example text This is some example text</p>
             <p><em>This is some example text This is some example text This is some example text</em></p>
             <p>This is some example text This is some example text This is some example text</p>";


    $emStartPos = strpos($text,"<em>");
    $emEndPos = strpos($text,"</em>");

    if ($emStartPos && $emEndPos) {
        $emEndPos += 5; //remove <em> tag aswell
        $len = $emEndPos - $emStartPos;

        $text = substr_replace($text,'',$emStartPos,$len);
    }

?>

这将删除标签之间的所有内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读