Reputation: 1
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
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
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