PHP从字符串中删除html标记

Wiz*_*ard 9 html php

我有字符串:

<p justify;"="">Vers­lo cent­rai Lie­tu­vos ne­kil­no­ja­mo­jo turto pl?t­ros aso­cia­ci­jos kon­kur­se  ...</p>
Run Code Online (Sandbox Code Playgroud)

并希望删除标签

<p justify;"=""></p>
Run Code Online (Sandbox Code Playgroud)

我的代码:

$content = strip_tags($text, '<p>');
Run Code Online (Sandbox Code Playgroud)

但我得到空字符串:string(0) "",我做错了什么?

Tor*_*tto 17

试着这样说吧

$content = strip_tags($text);
Run Code Online (Sandbox Code Playgroud)

或者您可以使用正则表达式来执行此操作:

$content = preg_replace('/<[^>]*>/', '', $text);
Run Code Online (Sandbox Code Playgroud)

通过这个$content = strip_tags($text, '<p>');你允许<p>字符串中的标记.

有关详细信息,请参阅http://php.net/manual/en/function.strip-tags.php链接


小智 5

由于 HTML 格式不佳,您可能需要编写自己的正则表达式来删除标签或在尝试删除标签之前清理 HTML。

您可以尝试删除所有“看起来像”标签的内容:

$str = preg_replace("/<.*?>/", " ", $str);
Run Code Online (Sandbox Code Playgroud)