Regular expression for validation of a facebook page url
php, regex
Solution
`parse_url()` can help you with that.
<?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>";
}
Problem
I need to validate the facebook page url which should not consider http/https or www given 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 following should not be accepted or invalid: ``` 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'd made following regular expression, but it is not checking the User Name or Page Name which is the only problem. Rest is working fine: ``` /^(https?:\/\/)?((w{3}\.)?)facebook.com\/(([a-z\d.]{5,})?)$/ ``` I am very new to Regular Expression, so don't have much idea about it. Any type of help would be appreciable. Thanks in advance.