Greg Reed
Greg Reed

Reputation: 45

Regular expressions PHP preg_match_all

Hi I am trying to use preg_match_all() to extract the number in bold out of an image URL...

http://profile.ak.fbcdn.net/hprofile-ak-snc4/174844_39677118233_8277870_t.jpg

Could someone please help me with the regular expression needed as I am stumped.

I've used this so far:

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

Which is just giving me an array of the full links.

Hope someone can help, thanks!!!

Upvotes: 1

Views: 1054

Answers (3)

Larry G. Wapnitsky
Larry G. Wapnitsky

Reputation: 1246

Try this:

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

I've separated it out onto multiple lines for easier reading. You will want to use capture group 4

Or (just minimized it a bit)

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

and use capture group 2

Upvotes: 0

vbence
vbence

Reputation: 20333

This will give you all occurrences:

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

Upvotes: 1

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

Number you have bolded should be contained in $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);

Upvotes: 1

Related Questions