开发者

PHP preg_match_all with Regular Expression applied to IMDB actor page

Hard question (for me at least) hope someone is able to help me as I already got lots of help here before! I have this code to get an actor image from the iMDB site

$string = FetchPage($url);
$image_regex_src_url = '/<td id="img_primary"[^>]*'. 'src=[\"|\'](.*)[\"|\']/Ui';
$img_tag_array = $out[0];
$image_regex_src_url = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
$images_url_array = $out[1];

Take Kevin Costner as example here: http://www.imdb.com/name/nm0000126/

I'm trying to adapt my code to fetch into a variable the integer of oscars won so far from this line: "Won 2 Oscars." and to another variable his birth date from the lines "Born:Kevin Michael Costner January 18, 1955 in Lynwood, California, USA"

To end with something like this:

$actor_oscars = 2;
$actor_birthdate = "January 18, 1955";

The thing is, my knowledge of开发者_JAVA技巧 Regular Expressions is very very limited and I already tried to make this alone (on a trial and error basis) and failed completely! Any good soul out there to help me?

PS: I tried to put the code here on stackoverflow to look pretty but even with that I seem not to succeed at all!

Thanks in advance!


After each preg_match line $matches[1] will contain the desired result

Image URL:

preg_match( '/<td[^>]*id="img_primary".+?<img[^>]*src="([^"]+)"/s', $str, $matches );

Oscars Won:

preg_match( '/Won\s(\d+)\sOscars\./', $str, $matches );

Birth Month-Day:

preg_match( '/<a href="[^"]*birth_monthday[^"]*">(.+?)<\/a>/', $str, $matches );

Birth Year:

preg_match( '/<a href="[^"]+birth_year[^"]+">(.+?)<\/a>/', $str, $matches );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜