Php 从HTML内容中删除

Php 从HTML内容中删除,php,preg-replace,strip-tags,Php,Preg Replace,Strip Tags,标记 我试图在网站上显示一个干净的段落,但是由于内容的中间,我没有得到预期的输出。我已经尝试了以下方法,但没有一个有效,我仍然在绕开我的内容 $story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type spec

标记 我试图在网站上显示一个干净的段落,但是由于内容的中间,我没有得到预期的输出。我已经尝试了以下方法,但没有一个有效,我仍然在绕开我的内容

$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";


 1. $story = strip_tags($story, '<p>');
 2. $story=str_ireplace('<p>','',$story);
 3. $story=str_ireplace('</p>','',$story);
 4. $story = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $story);
我的密码有没有漏掉什么?我的预期产出是

答:自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书

用str_replace试试这种方法

用str_replace试试这种方法


PHP具有内置函数,可以从字符串中删除HTML标记。最好使用strip_标记而不是str_替换,下面是一个示例

     $story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";
;
        $a = strip_tags($story);
        $b = strip_tags($story, "<strong><em>"); // can also pass the tags that you don't want to remove

PHP具有内置函数,可以从字符串中删除HTML标记。最好使用strip_标记而不是str_替换,下面是一个示例

     $story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";
;
        $a = strip_tags($story);
        $b = strip_tags($story, "<strong><em>"); // can also pass the tags that you don't want to remove
使用str_ireplace函数

$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";

echo $text=str_ireplace('<p>','',$story);
演示:

使用str\u ireplace函数

$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";

echo $text=str_ireplace('<p>','',$story);
演示:

有一个内置函数可以删除php中的所有html标记。带标签


有一个内置函数可以删除php中的所有html标记。strip_标记

$story是否只包含html标记而不包含其他html标记?如果将作为strip_标记的第二个参数传递,则告诉函数允许该标记,而不是删除它。如果要去除所有标记,只需省略第二个参数。如果要保留其他html标记,则应将其作为第二个参数传递。您的预期输出是什么?@vivek_23我确实在内容中有标记,但现在,我只想从内容$story中删除标记。是否$story只包含html标记而没有其他html标记?如果您将作为第二个参数传递给strip_标记,那么您告诉函数允许该标记,而不是删除它。如果要去除所有标记,只需省略第二个参数。如果要保留其他html标记,则应将其作为第二个参数传递。您的预期输出是什么?@vivek_23我的内容中有标记,但现在,我只想从内容$story中删除标记。可能是我看到的演示的副本,它工作得很好,但我的代码中没有得到相同的输出。我正在获取变量$story中的Unicode字符,这会给我带来问题吗?您给出的每个答案在这里都非常有效:但不是在我的代码中。请看这里,我在那里查看了,但没有任何用处。是的,我尝试在循环外使用Unicode字符,但我在foreach循环中显示$story,但这不起作用。我看到了演示,它工作得非常好,但我的代码中没有得到相同的输出。我正在获取变量$story中的Unicode字符,这会给我带来问题吗?您给出的每个答案在这里都非常有效:但不是在我的代码中。请看这里,我在那里查看了,但没有任何用处。是的,我尝试在循环外使用Unicode字符,但我在foreach循环中显示$story,但这不起作用。
$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 
    1500s, when an unknown printer took a galley of type and scrambled it to make a 
    type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text 
    ever since the 1500s, when an unknown printer took a galley of type and scrambled 
    it to make a type specimen book.</p>";

$story = strip_tags($story) ;
echo $story;