带有正则表达式的PHP preg_match_all应用于IMDB actor页面

时间:2011-07-05 19:19:49

标签: php regex expression

难题(至少对我来说)希望有人能够帮助我,因为我之前已经得到了很多帮助!我有这个代码来从iMDB网站获取一个演员图像

$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];

以Kevin Costner为例:http://www.imdb.com/name/nm0000126/

我正在尝试调整我的代码来获取一个变量,这个变量是迄今为止从这一行获得的整数奥斯卡奖:“赢得了2次奥斯卡奖”。另一个变量是他的出生日期从“Born:Kevin Michael Costner”出生 1955年1月18日,美国加利福尼亚州林伍德市“

以这样的结尾结束:

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

问题是,我对正则表达式的了解非常有限,而且我已经尝试单独制作(基于试验和错误)并完全失败!有没有好心灵来帮助我?

PS:我试着将这些代码放在stackoverflow上看起来很漂亮,但即便如此,我似乎根本没有成功!

提前致谢!

1 个答案:

答案 0 :(得分:2)

在每个preg_match行之后,$ matches [1]将包含所需的结果

图片网址:

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

获得奥斯卡奖:

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

出生月 - 日:

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

出生年份:

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