0

我正在从数据库中提取一些内容,其中一些内容可能有图像,但我只需要提取文本内容而不回显内容中可能存在的任何图像。

样本:

   $nws = '<h3>Just text Just text Just text Just text Just text Just text.</h3>

    <p><img alt="" src="/opt_file/images/09062013C-pix1.jpg" style="width: 400px; height: 231px;" /></p>

<img alt="" src="/opt_file/images/another-pix2.png" style="width: 300px; height: 150px;" />

    <p>Just text Just text Just text Just text Just text Just text.</p>

    <p>Just text Just text Just text Just text Just text Just text.</p>

    <p><strong>Sure these are just text.</strong></p>';

    $nws = str_replace( '<img alt="" src="" />', "" , $nws );

    echo $nws;

我尝试使用 str_replace 撕掉其中的任何图像标签或图像,但它不起作用。

此外,这些内容和图像是动态的,一个特定的可能有多个图像。

真的需要解决这个问题,非常感谢获得帮助。

谢谢!

4

2 回答 2

1

使用正则表达式,

$nws = preg_replace("/<img[^>]+\>/i", "", $nws); 
于 2013-06-25T11:35:17.220 回答
1

使用strip_tags()函数,示例:

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

要去除特定的 html 标签,请使用此功能:

echo strip_tags($text,'<p>');
于 2013-06-25T11:36:06.063 回答