2012-11-02 49 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;