正则表达式PHP preg_match_all

时间:2011-03-28 13:52:25

标签: php preg-match-all

您好我正在尝试使用preg_match_all()从图片网址中提取粗体数字...

http://profile.ak.fbcdn.net/hprofile-ak-snc4/174844_ 39677118233 _8277870_t.jpg

有人可以帮我解决所需的正则表达,因为我很难过。

到目前为止我用过这个:

preg_match_all("(http://profile.ak.fbcdn.net/hprofile-ak-snc4/.*_t.jpg)siU", $this->html, $matching_data);
return $matching_data[0];
}

这只是给我一系列完整的链接。

希望有人能提供帮助,谢谢!

3 个答案:

答案 0 :(得分:1)

您加粗的数字应包含在$matches[$n][3] ...

preg_match_all("#http://profile\.ak\.fbcdn\.net/(.*?)/([0-9]+)_([0-9]+)_([0-9]+)_t\.jpg#is", $string, $matches);
print_r($matches);

答案 1 :(得分:1)

这将为您提供所有事件:

$matches = preg_match_all ('!/hprofile-ak-snc4/[0-9]+_([0-9]+)[^/]+?\.jpg!i', $txt);
print_r ($matches);

答案 2 :(得分:0)

试试这个:

([a-z][a-z0-9+\-.]*:(//[^/?#]+)?)?
([a-z0-9\-._~%!$&'()*+,;=:@/]*)
(?:(?:\d+_)(\d+)(?:_\d+))\3

我将它分成多行以便于阅读。您将需要使用捕获组4

或者(稍微减少一点)

(?:[a-z][a-z0-9+\-.]*:(?://[^/?#]+)?)?
([a-z0-9\-._~%!$&'()*+,;=:@/]*)
(?:(?:\d+_)(\d+)(?:_\d+))\1

并使用捕获组2