The simple solution and quite readable: Use the entire string as a regex, use ()
around what you want to match:
// $tmp[1] = www or nothing
// $tmp[2] = "user" (i.e nokia)
// $tmp[3] = album id?
// $tmp[4] = photos
// $tmp[5] = Long url as requested
function extract_id_from_album_url($url) {
preg_match('/https?:\/\/(www.)?facebook\.com\/([a-zA-Z0-9_\- ]*)\/([a-zA-Z0-9_\- ]*)\/([a-zA-Z0-9_\.\-]*)\/([a-zA-Z0-9_\-]*)(\/\?type=1&theater\/)?/i', $url, $tmp);
return isset($tmp[5]) ? $tmp[5] : false;
}
Backslashes are needed to ensure the .
is seen as a literal (and not regex syntax). Questionmarks to allow optional urls. Using more regex syntax can make the matching "query" much shorter and extendable, but also makes it harder to read.