"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Match URLs Using Regular Expressions?

How to Match URLs Using Regular Expressions?

Published on 2024-11-10
Browse:650

How to Match URLs Using Regular Expressions?

Matching URLs with Regular Expressions

Regular expressions can be daunting initially, but they offer powerful pattern-matching capabilities for diverse data types. In the context of extracting URLs, a flexible pattern is necessary to accommodate variations in URL formats.

One robust regular expression that can capture URLs with or without leading protocols (e.g., "http://www" or "www") is:

((https?|ftp)://)? // Optional SCHEME
([a-z0-9 !*(),;?&=$_.-] (:[a-z0-9 !*(),;?&=$_.-] )?@)? // Optional User and Pass
([a-z0-9\-\.]*)\.(([a-z]{2,4})|([0-9]{1,3}\.([0-9]{1,3})\.([0-9]{1,3}))) // Host or IP address
(:[0-9]{2,5})? // Optional Port
(/([a-z0-9 $_%-]\.?) )*/? // Path
(\?[a-z &\$_.-][a-z0-9;:@&%= /$_.-]*)? // Optional GET Query
(#[a-z_.-][a-z0-9 $%_.-]*)? // Optional Anchor

To use this expression in PHP, enclose it in double quotes and pass it to the preg_match function along with the URL you want to evaluate. For example:

$url = 'www.example.com/etcetc';
if (preg_match("~^$regex$~i", $url)) {
    echo 'Matched URL without protocol';
}

Similarly, for URLs with protocols:

$url = 'http://www.example.com/etcetc';
if (preg_match("~^$regex$~i", $url)) {
    echo 'Matched URL with protocol';
}

This pattern should cover a wide range of URL formats while also protecting against potential malicious input containing characters such as "/".

Release Statement This article is reprinted at: 1729557858 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3