Home >Backend Development >PHP Tutorial >How to Detect URLs of Varying Formats Using Regular Expressions?
Regular expressions provide a powerful way to extract data from complex strings, including URLs. Whether you're working with URLs containing "http://www" prefixes or not, a comprehensive regular expression can cater to your needs.
The following expression has been crafted to match URLs with and without the "http://www" prefix:
((https?|ftp)://)?([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?([a-z0-9\-\.]*)\.(([a-z]{2,4})|([0-9]{1,3}\.([0-9]{1,3})\.([0-9]{1,3})))(:[0-9]{2,5})?(/([a-z0-9+$_%-]\.?)+)*/?(\?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?(#[a-z_.-][a-z0-9+$%_.-]*)?
This expression incorporates the following components:
To utilize this expression, you can employ the PHP code below:
if(preg_match("~^$regex$~i", 'www.example.com/etcetc', $m)) var_dump($m); if(preg_match("~^$regex$~i", 'http://www.example.com/etcetc', $m)) var_dump($m);
This code will successfully match both URLs, regardless of the presence of the "http://www" prefix.
The above is the detailed content of How to Detect URLs of Varying Formats Using Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!