Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP regex match all urls [duplicate]

Tags:

regex

php

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

like image 933
André Figueira Avatar asked May 10 '13 11:05

André Figueira


2 Answers

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);
like image 66
Lakatos Gyula Avatar answered Oct 12 '22 00:10

Lakatos Gyula


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.

like image 42
cryptic ツ Avatar answered Oct 12 '22 02:10

cryptic ツ