2013-06-25 37 views
0

我从数据库中后台打印出一些内容,其中一些内容可能具有图像,但我只需要仅提取文本内容而不回显任何可能的图像内容。PHP:从正在回显的内容中剥离图像标签

样品:

$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函数撕掉内的任何图像标签或图像,但它只是将无法正常工作。

此外,这些内容和图像是动态的,并且有可能具有多个图像。

真的需要解决这个问题,将非常感激得到帮助。

谢谢!

回答

1

使用正则表达式,

$nws = preg_replace("/<img[^>]+\>/i", "", $nws); 
+0

好极了!按照我需要的方式工作......感谢百万... – John

+0

很高兴为您提供帮助。请不要忘记通过点击左侧的复选标记来接受我的答案。 – Rikesh

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>'); 
+0

OP只希望''标签被移除,尽管 –