2013-06-25 39 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只希望''標籤被移除,儘管 –