开发者

take facebook page url and store id and slug separately

I'm developing a web app where users enter their facebook page url either in this format: http://www.facebook.com/pages/Graffiti/119622954518

or http://www.facebook.com/thefirkinandfox

With php - how do I detect which format automatically, then split (explode?) the parts (the slug and the id or just the slug if the second version).

There is sometimes query data at the end of the url when viewing your own facebook page as an administrator, how do I detect and remove that? I think the answer will be regex of some kind - but I've really only used this to make sure an input is email and sti开发者_如何学JAVAll didn't understand it that well... thanks in advance.

Possible entires may or may not include http:// at the beginning... I'd like to account for this...


If you want to use one regexp, try this:

$url = 'www.facebook.com/pages/Graffiti/119622954518';
if(preg_match('@^(https?://)?(www\.)?facebook\.com/((pages/([^/]+)/(\d+))|([^/]+))@', $url, $matches)) {
  $slug = isset($matches[5]) ? $matches[5] : (isset($matches[7]) ? $matches[7] : null);
  $id = isset($matches[6]) ? $matches[6] : null;
}


Two parts:

^http://www.facebook.com/pages/([^/]+)/([^/]+)(?:\?.*)$

If the first one doesn't match, use this:

^http://www.facebook.com/([^/]+)(?:\?.*)$

The explosion, you mention is the value of the capturing group.

So the code might look something like this:

$subject = "my string";
if (preg_match ('#^http://www.facebook.com/pages/([^/]+)/([^/]+)(?:\?.*)$#', $subject))
   print ($groups[1] + ' ' + $groups[1]);
else if (preg_match ('#^http://www.facebook.com/([^/]+)(?:\?.*)$#', $subject))
   print ($groups[1]);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜