PHP的preg_match帮助获得来自Facebook的URL的用户名/ ID [英] php preg_match help getting facebook username/id from URL

查看:284
本文介绍了PHP的preg_match帮助获得来自Facebook的URL的用户名/ ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个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);

该模式是通用的,因此相匹配:


  1. HTTP和HTTPS URL的。

  2. 网址的使用或不使用 WWW preFIX。

  3. URL的带或不带尾随 /

  4. 匹配的Facebook的数字ID的。

  5. 匹配<一个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:

  1. http and https URL's.
  2. URL's with or without the www prefix.
  3. URL's with or without the trailing /.
  4. Matches Facebook numerical ID's.
  5. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆