alfred.alaan
alfred.alaan

Reputation: 1

remove img tag src is a url using preg_replace

Hi I would like to remove a img tag with it's src is a url in a content using preg_replace

ex.

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

so output would be:

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

but best output would be if possible is :

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

Upvotes: 0

Views: 741

Answers (2)

Samuel Cook
Samuel Cook

Reputation: 16818

preg_match_all() would work here, but with HTML it's not the most efficient.

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

The easiest way is to just use strip_tags()

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

Upvotes: 2

otporan
otporan

Reputation: 1355

You can do that this way:

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

Upvotes: 1

Related Questions