从 HTML 内容中删除 <p> 标签
Remove <p> tag from HTML content
我试图让一个干净的段落显示在网站上,但是由于内容中间的 <p></p>
,我没有得到预期的输出。我尝试了以下方法,但其中 none 成功了,我的内容仍然 <p></p>
。
$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);
我是否遗漏了代码中的任何内容?我的预期输出是
Answer: 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. 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.
用 str_replace()
试试这个方法
<?php
$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>";
$result = str_replace(['<p>','</p>'],['',''],$story);
echo $result;
?>
OR with strip_tags() 删除所有标签,顺便说一句,用这个方法你可以指定 allowable_tags
$result = strip_tags($story);
PHP 具有从字符串中删除 HTML 标签的内置函数。最好使用strip_tags()
而不是str_replace()
,这里有一个例子
$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);
$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;
有一个内置函数可以删除 php 中的所有 html 标签。 strip_tags()
我试图让一个干净的段落显示在网站上,但是由于内容中间的 <p></p>
,我没有得到预期的输出。我尝试了以下方法,但其中 none 成功了,我的内容仍然 <p></p>
。
$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);
我是否遗漏了代码中的任何内容?我的预期输出是
Answer: 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. 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.
用 str_replace()
<?php
$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>";
$result = str_replace(['<p>','</p>'],['',''],$story);
echo $result;
?>
OR with strip_tags() 删除所有标签,顺便说一句,用这个方法你可以指定 allowable_tags
$result = strip_tags($story);
PHP 具有从字符串中删除 HTML 标签的内置函数。最好使用strip_tags()
而不是str_replace()
,这里有一个例子
$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);
$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;
有一个内置函数可以删除 php 中的所有 html 标签。 strip_tags()