I need help creating a Regex that will match all urls for example, please do not close question as a duplicate as I have been looking for what i need for a long time, and none of the answers i have seen have given an answer that solves my problem.
website.com
www.website.com
http://www.website.com
http://website.com
https://www.website.com
https://website.com
with also anything trailing
www.website.com/path-to-something
I am coding something that shortens any url, but to do so, first i need to match them all.
Thanks
This one match correctly all you posted:
preg_match_all('#[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?#si', $targetString, $result);
You want to use something like this:
$string = 'www.blah.com';
$temp_string = (!preg_match('#^(ht|f)tps?://#', $string)) // check if protocol not present
? 'http://' . $string // temporarily add one
: $string; // use current
if (filter_var($temp_string, FILTER_VALIDATE_URL))
{
echo 'is valid';
} else {
echo 'not valid';
}
This uses PHP's build in URL validation. It will first check to see if a protocol is present, if it is not it will temporarily add one to a string to be checked then run it through validation. This is accurate unlike the currently accepted answer.
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