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!

有帮助吗?

解决方案

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]);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top