删除img标签src是一个使用preg_replace的url

时间:2012-11-02 17:23:05

标签: php regex html

您好我想删除一个img标签,其中src是使用preg_replace的内容中的网址

离。

    $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 ";

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;