开发者

Getting div data without using DOM [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Getting DIV content with Regular Expression

Let me first tell you that DOM is not an option on this one. I simply have the html :

className">Name</div>......</div>....</div>

Now, i have created a regular expression like :

$match_count = preg_match_all('/className\">(.*)\<\/div\>/', $page, $matches);

This would seem fine to me, but for some reason, it gets more data than expected. That is, it finishes some closing divs later. How can i restrict it so that it gets the data only inside the first closin开发者_Python百科g div ?


$match_count = preg_match_all('/className">(.*?)<\/div>/', $page, $matches);

use non greedy selector .*?


Use preg_match instead. It will stop searching after the first matched pattern.


This works:

$match_count = preg_match_all('/className\">(.*)\<\/div\>/', $page, $matches);

The U pattern modifier will make sure it finds the smallest possible match, not the biggest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜