문제

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