user3474451
user3474451

Reputation: 47

Get data from div classes

How I can get data from div classes?

Example: A <div class=ab>1 &nbsp; 2</div>b <div class=ab>3 &nbsp; 4</div>c.

I want: 1 &nbsp; 2, 3 &nbsp; 4 etc - between <div class=ab> and </div>.

Second example: http://www.imdb.com/title/tt29747">

I want: tt29747 - between http://www.imdb.com/title/ and ">.

With strstr all good, except that I get only the first result. I tried some solutions founded here but no succes, regular expressions beyond me. Thank you!

Upvotes: 2

Views: 119

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Try parsing the HTML using DOMDocument() instead of regex.

However, here is the regex to parse assuming there will be no nested div:

$html= 'Example: Lorem <div class=ab>1 &nbsp; 2</div>ipsum <div class=ab>3 &nbsp; 4</div>dolor.';
preg_match_all('|<div class=ab>([^<]*)</div>|i', $html, $m);
print_r($m[1]);

And for parsing the title id:

$html = 'http://www.imdb.com/title/tt29747">';
preg_match('|imdb.com/title/(tt\d+)|i', $html, $m);
print_r($m[1]);

Upvotes: 1

Related Questions