0

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 ";
quinestor
  • 1,432
  • 4
  • 19
  • 40

2 Answers2

2

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;
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
1

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;
otporan
  • 1,345
  • 2
  • 12
  • 29