PHP的preg_match帮助获得来自Facebook的URL的用户名/ ID [英] php preg_match help getting facebook username/id from URL
问题描述
我想从一个Facebook网址Facebook的用户名或ID,并想知道如果你能帮助我。我敢肯定,这只是一个简单的正则表达式的功能,但我已经尝试似乎并不能做到这一点我自己。
这就是我一直在尝试从URL中得到的。
http://www.facebook.com/ 名为myusername /
块引用>或数字ID
http://www.facebook.com/ 2285652 /
块引用>我会很感激,如果有人可以帮助我通过 PHP
实现这一目标解决方案<$p$p><$c$c>$p$pg_match('#https?\\://(?:www\\.)?facebook\\.com/(\\d+|[A-Za-z0-9\\.]+)/?#',$str,$matches);
该模式是通用的,因此相匹配:
- HTTP和HTTPS URL的。
- 网址的使用或不使用
WWW
preFIX。- URL的带或不带尾随
/
。- 匹配的Facebook的数字ID的。
- 匹配<一个href=\"https://www.facebook.com/help/?faq=105399436216001#What-are-the-guidelines-around-creating-a-custom-username?\">Facebook's对于用户名规则的。
用户名只能包含字母数字字符(A-Z,0-9)或
句点(。)。
块引用>I am trying to get the facebook username or id from a facebook url and was wondering if you could help me. I'm sure it's just a simple regex function but I have tried and can't seem to do it myself.
Here's what I've been trying to get from the url.
http://www.facebook.com/myusername/
or the numerical id
http://www.facebook.com/2285652/
I would be very grateful if anyone could help me achieve this via PHP
解决方案preg_match('#https?\://(?:www\.)?facebook\.com/(\d+|[A-Za-z0-9\.]+)/?#',$str,$matches);
The pattern is versatile so it matches:
- http and https URL's.
- URL's with or without the
www
prefix.- URL's with or without the trailing
/
.- Matches Facebook numerical ID's.
- Matches Facebook's rules for usernames.
Usernames can only contain alphanumeric characters (A-Z, 0-9) or a period (".").
这篇关于PHP的preg_match帮助获得来自Facebook的URL的用户名/ ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!