ngplayground
ngplayground

Reputation: 21627

PHP Preg_match Matching a class and getting content after

$str = '<div class="rss"><img src="http://www.wired.com/images_blogs/gadgetlab/2013/10/1125_hbogo_660-660x436.jpg" alt="You Can Now Get HBO GO Without Paying for Other Channels">
</div>Fans of';

I'm trying to get hold of the text after the <div class="rss"></div> but each expression I use doesn't seem to work.

matching .rss

if(preg_match('/^(<div class=\"rss\">[\S\s]+?</div>([\S\s]*)$/i', $item_content, $matches)) { 

Could someone please help with this expression?

Originally I had this expression to match an image tag instead of a div and this worked fine by using

if(preg_match('/^(<img[\S\s]+?>)([\S\s]*)$/i', $item_content, $matches)) {

Upvotes: 0

Views: 2400

Answers (2)

jacouh
jacouh

Reputation: 8741

This may help:

<?php
$item_content = '<div class="rss"><img src="http://www.wired.com/images_blogs/gadgetlab/2013/10/1125_hbogo_660-660x436.jpg" alt="You Can Now Get HBO GO Without Paying for Other Channels">
</div>Fans of';

if(preg_match('/^(<div class=\"rss\">[\S\s]+?<\/div>)([\S\s]*)$/i', $item_content, $matches)) {
  $div = $matches[1];
  $text = $matches[2];

  echo "<textarea style=\"width: 600px; height: 300px;\">";
  echo $div . "\n";
  echo $text . "\n";
  echo "</textarea>";
}
?>

Upvotes: 0

revo
revo

Reputation: 48731

I didn't go deeply for the regex but yours work well with just solving some syntax problems.

It should be:

^<div class=\"rss\">[\S\s]+?<\/div>([\S\s]*)$/i

Live demo

Upvotes: 1

Related Questions