2012-11-02 101 views
0

嗨一個網址我想刪除一個img標籤與它的src在使用的preg_replace刪除img標籤src爲使用的preg_replace

EX內容的URL。

$content = "<center><img src="http://example.net/wp-content/uploads/2012/10/cell-degeneration-contn.jpg" alt="" title="cell-degeneration-contn" width="950" height="272" class="alignnone size-full wp-image-100" /></center><h2>A first-in-class approach to stop & reverse </h2>"; 

所以輸出會是:

$content="<center></center><h2>A first-in-class approach to stop & reverse </h2>"; 

,但最佳的輸出是,如果可能的是:

$content="A first-in-class approach to stop & reverse "; 
+0

是否有* *使用的preg_replace? –

+1

歡迎來到堆棧溢出。正如Marc B指出的那樣,似乎正則表達式不是操作html文本的方式;只是澄清,如果你真的需要使用preg_replace – quinestor

回答

2

preg_match_all()就在這裏工作,但與HTML這不是最有效的。

$content = '<center><img src="http://example.net/wp-content/uploads/2012/10/cell-degeneration-contn.jpg" alt="" title="cell-degeneration-contn" width="950" height="272" class="alignnone size-full wp-image-100" /></center><h2>A first-in-class approach to stop & reverse </h2>'; 

preg_match("/<h2>(.*)<\/h2>/",$content,$matches); 
$output = $matches[1]; 
echo $output; 

最簡單的方法是隻使用strip_tags()

$output = strip_tags($content); 
echo $output; 
+0

它的工作原理!非常感謝! –

+1

爲什麼preg_match_all而不是preg_match? : - ?已更改的 – RezaSh

+0

。 –

1

你可以做到這一點是這樣的:

$content = "<center><img src='http://example.net/wp-content/uploads/2012/10/cell-degeneration-contn.jpg' alt='' title='cell-degeneration-contn' width='950' height='272' class='alignnone size-full wp-image-100' /></center><h2>A first-in-class approach to stop & reverse </h2>'"; 
    preg_match("/<h2>(.+)<\/h2>/", $content, $matches); 
    $match = $matches[1]; 
    echo $match;