Regular expression to check facebook page url

Do I need to check the facebook page url which should not consider HTTP / https or www, or not?

I mean the following should be accepted or valid :

www.facebook.com/ABCDE facebook.com/ABCDE http://www.facebook.com/ABCDE https://www.facebook.com/ABCDE 

And below should not be accepted or invalidated :

 http://www.facebook.com/ => User name/page name not given http://www.facebook.com/ABC => User name/page name should have the minimum length of 5. 

For the above requirement, I made the following regular expression, but it does not check the username or page name, which is the only problem. Rest works fine:

 /^(https?:\/\/)?((w{3}\.)?)facebook.com\/(([az\d.]{5,})?)$/ 

I am very new to regex, so I don't have much idea about this.

Any help would be noticeable.

Thanks in advance.

+6
source share
2 answers

parse_url() can help you with this.

 <?php $array = array( "www.facebook.com/ABCDE", "facebook.com/ABCDE", "http://www.facebook.com/ABCDE", "https://www.facebook.com/ABCDE", "http://www.facebook.com/", "http://www.facebook.com/ABC" ); foreach ($array as $link) { if (strpos($link, "http") === false) { $link = "http://" . $link; //parse_url requires a valid URL. A scheme is needed. Add if not already there. } $url = parse_url($link); if (!preg_match("/(www\.)?facebook\.com/", $url["host"])) { //Not a facebook URL echo "FALSE!"; } elseif (strlen(trim($url["path"], "/")) < 5) { //Trailing path (slashes not included) is less than 5 echo "FALSE!"; } else { //None of the above echo "TRUE"; } echo "<br>"; } 
+2
source

Try this (not tested, should work)

 '~^(https?://)?(www\.)?facebook\.com/\w{5,}$~i' 

\ w is like [a-zA-Z0-9 _]

Robert

-1
source

All Articles