PHP从字符串中删除html标记
我有字符串:PHP从字符串中删除html标记,php,html,Php,Html,我有字符串: <p justify;"="">Verslo centrai Lietuvos nekilnojamojo turto plėtros asociacijos konkurse ...</p> 我的代码: $content = strip_tags($text, '<p>'); $content=strip_标签($text,); 但是我得到了空字符串:string(0)”“,我做错了什么?由于HTML格
<p justify;"="">Verslo centrai Lietuvos nekilnojamojo turto plėtros asociacijos konkurse ...</p>
我的代码:
$content = strip_tags($text, '<p>');
$content=strip_标签($text,);
但是我得到了空字符串:
string(0)”“
,我做错了什么?由于HTML格式很差,您可能需要编写自己的regexp来删除标记,或者在尝试删除标记之前清理HTML
您可以尝试删除所有“看起来”像标记的内容:
$str=preg\u replace(“//”,“,$str);
试着这样说
$content = strip_tags($text);
或者您可以使用正则表达式这样做:
$content = preg_replace('/<[^>]*>/', '', $text);
$content=preg_replace('/]*>/',''.$text);
通过这个$content=strip_标签($text,)代码>允许在字符串中使用
标记
有关更多信息,请参见链接,因为您的HTML格式不正确,您可以选择preg\u replace()
方法:
$text = '<p justify;"="">Verslo centrai Lietuvos nekilnojamojo turto plėtros asociacijos konkurse ... </p>';
$content = preg_replace('/<[^>]*>/', '', $text);
var_dump($content);
// string(108) "Verslo centrai Lietuvos nekilnojamojo turto plėtros asociacijos konkurse ... "
$text=”这将删除所有内容—标记、ascii、换行符,但纯文本除外:
strip_tags(preg_replace('/<[^>]*>/','',str_replace(array(" ","\n","\r"),"",html_entity_decode($YOUR_STRING,ENT_QUOTES,'UTF-8'))));
strip_标记(preg_replace('/]*>/','',str_replace(数组(“,”\n“,“\r”),'',html_实体解码($YOUR_字符串,entu引号,'UTF-8'));
这将替换所有html标记,
preg_replace('/',$replacement,$string);
从PHP 7.4.0开始,strip_tags()也可以接受带有允许标记的数组
那么这个,
<?php
$html = '<div id="my-div"><p>text<strong><a href="#link"></a></strong></p></div>';
echo strip_tags($html, ['p', 'a']); //accept p and a tags
它不起作用。我刚刚在我的盒子上测试了它,第一个不匹配的报价就是问题所在。即使是那个愚蠢的分号也可以用strip_标签,只要他没有第一句话…@mihaiorga Ya同意你的说法。这可能是打字错误。真的吗?为什么wordpress会使用不匹配的引用?这对我来说很有用。strip_tags仅将标记转换为其他字符这在删除'UTF-8'
后对我有效,因为第二个参数需要是整数
strip_tags(preg_replace('/<[^>]*>/','',str_replace(array(" ","\n","\r"),"",html_entity_decode($YOUR_STRING,ENT_QUOTES,'UTF-8'))));
preg_replace('/<(|\/)(?!\?).*?(|\/)>/',$replacement,$string);
<?php
$html = '<div id="my-div"><p>text<strong><a href="#link"></a></strong></p></div>';
echo strip_tags($html, ['p', 'a']); //accept p and a tags
<p>text<a href="#link"></a></p>