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>

したがって、tagをallow_tags 除く<img>すべての 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 に答える