2

可能重复:
PHP - 从字符串中删除 <img> 标记

我的内容是这样的:

$content = '<p>&nbsp;</p>
<p><img width="645" height="450" src="/proshore/userfiles/image/1.jpg" alt="" /></p>
<p>An explorer adventures into an unknown world, yet it seems that he has been there      before. A short animated film directed by Malcolm Sutherland in 2010. With music by Alison Melville and Ben Grossman, and foley by Leon Lo. Sound design / mix by Malcolm Sutherland.</p>
<p>The animation is all hand-drawn; a mix of drawing / pastels on paper and digital animation with Toonboom Studio and a wacom cintiq tablet, assembled in After Effects 7 and edited in Sony Vegas 8.</p>
<p>&nbsp;</p>';

我希望输出忽略<img>标签。我用 preg_replace 尝试了一些混乱,但没有奏效。如果有人能向我解释它是如何工作的,那将是一个很大的帮助。

4

3 回答 3

1

如果您不被迫使用正则表达式来解析 HTML 内容,那么我建议您使用 PHP 的strip_tags()函数及其allowed_tags参数。

这将告诉 PHP 删除所有 html 标记并保留您在allowed_tags.

文档中获取的示例用法-

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text); 
// output : Test paragraph. Other text

// Allow <p> and <a>
echo strip_tags($text, '<p><a>'); 
// output - <p>Test paragraph.</p> <a href="#fragment">Other text</a>

allow_tags 因此,如果您只是指定除<img>tag之外的所有 HTML 标记,您应该得到您需要的结果 - 只需<img>从字符串中删除标记。

于 2012-07-16T11:09:52.550 回答
1

尝试:

 $content = preg_replace("/<img[^>]+\>/i", " ", $content);
于 2012-07-16T11:09:55.047 回答
0

<img.+?((/>)|(</img>))用空字符串替换这个正则表达式

于 2012-07-16T11:12:05.310 回答