Pessoalmente, eu gostaria de usar expressões regulares aqui. O código abaixo funcionou perfeitamente para mim.
$baseUrl = url('/'); // for my case https://www.xrepeater.com
$posted_url = "home";
// Test with one by one
/*$posted_url = "/home";
$posted_url = "xrepeater.com";
$posted_url = "www.xrepeater.com";
$posted_url = "http://www.xrepeater.com";
$posted_url = "https://www.xrepeater.com";
$posted_url = "https://xrepeater.com/services";
$posted_url = "xrepeater.dev/home/test";
$posted_url = "home/test";*/
$regularExpression = "((https?|ftp)\:\/\/)?"; // SCHEME Check
$regularExpression .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass Check
$regularExpression .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP Check
$regularExpression .= "(\:[0-9]{2,5})?"; // Port Check
$regularExpression .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path Check
$regularExpression .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query String Check
$regularExpression .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor Check
if(preg_match("/^$regularExpression$/i", $posted_url)) {
if(preg_match("@^http|https://@i",$posted_url)) {
$final_url = preg_replace("@(http://)+@i",'http://',$posted_url);
// return "*** - ***Match : ".$final_url;
}
else {
$final_url = 'http://'.$posted_url;
// return "*** / ***Match : ".$final_url;
}
}
else {
if (substr($posted_url, 0, 1) === '/') {
// return "*** / ***Not Match :".$final_url."<br>".$baseUrl.$posted_url;
$final_url = $baseUrl.$posted_url;
}
else {
// return "*** - ***Not Match :".$posted_url."<br>".$baseUrl."/".$posted_url;
$final_url = $baseUrl."/".$final_url; }
}