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:
www
prefix./
.Usernames can only contain alphanumeric characters (A-Z, 0-9) or a period (".").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With