I’m not very good at regular expressions at all.
I’ve been using a lot of framework code to date, but I’m unable to find one that is able to match a URL like http://www.example.com/etcetc
but also is able to catch something like www.example.com/etcetc
and example.com/etcetc
.
Any help would be great. Thanks guys!
For matching all kind of URLs following code should work:
<?php
$regex = "((https?|ftp)://)?"; // SCHEME
$regex .= "([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)[email protected])?"; // User and Pass
$regex .= "([a-z0-9\-\.]*)\.(([a-z]{2,4})|([0-9]{1,3}\.([0-9]{1,3})\.([0-9]{1,3})))"; // Host or IP
$regex .= "(:[0-9]{2,5})?"; // Port
$regex .= "(/([a-z0-9+$_%-]\.?)+)*/?"; // Path
$regex .= "(\?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?"; // GET Query
$regex .= "(#[a-z_.-][a-z0-9+$%_.-]*)?"; // Anchor
?>
Then, the correct way to check against the regex is as follows:
<?php
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);
?>
Courtesy:
Comments made by splattermania on PHP manual: http://php.net/manual/en/function.preg-match.php
Answer:
This works for me in all cases I had tested:
$url_pattern = '/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/';
Tests:
http://test.test-75.1474.stackoverflow.com/
https://www.stackoverflow.com
https://www.stackoverflow.com/
http://wwww.stackoverflow.com/
http://wwww.stackoverflow.com
http://test.test-75.1474.stackoverflow.com/
http://www.stackoverflow.com
http://www.stackoverflow.com/
stackoverflow.com/
stackoverflow.com
http://www.example.com/etcetc
www.example.com/etcetc
example.com/etcetc
user:[email protected]/etcetc
example.com/etcetc?query=aasd
example.com/etcetc?query=aasd&dest=asds
http://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match-url-with-or-without-http-www
http://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match-url-with-or-without-http-www/
Every valid internet URL has at least one dot, so the above pattern will simply try to find any at least two string chained by a dot, and has valid characters that URL may have.
Answer:
Try this:
/^http:\/\/|(www\.)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
It works exactly like the people want.
It takes with or with out http://
, https://
, and www
.
Answer:
You can use a question mark after a regular expression to make it conditional so you would want to use:
http:\/\/(www\.)?
That will match anything that has either http://www. or http:// (with no www.)
What you could do is just use a replace method to remove the above, thus getting you the domain. Depends on what you need the domain for.
Answer:
Try something like this:
.*([\w-]+\.)+[a-z]{2,5}(/[\w-]+)*
Answer:
I know this is an old post, but just contributing my solution which is a combination of some of the answers I’ve found here on stackoverflow.
/(https?://)?((?:(\w+-)*\w+)\.)+(?:[a-z]{2})(\/?\w?-?=?_?\??&?)+[\.]?([a-z0-9\?=&_\-%#])?/g
Matches something.com
, http(s)://
or www
. Does not match other [something]://
urls though, but for my purpose that’s not necessary.
The regex matches e.g.:
http://foo.co.uk/
www.regex.com/foo.html?q=bar$some=thi-ng,regex
regex.foo.com/blog
Answer:
Try this
$url_reg = /(ftp|https?):\/\/(\w+:?\w*@)?(\S+)(:[0-9]+)?(\/([\w#!:.?+=&%@!\/-])?)?/;
Answer:
you can try this:
r"(http[s]:\/\/)?([\w-]+\.)+([a-z]{2,5})(\/+\w+)? "
selection :
1. may be start with http:// or https:// (optional)
2. anything (word) end with dot (.)
3. followed by 2 to 5 character [a-z] 4. followed by “/[anything]” (optional)
5. followed by space
Answer:
If it does not have to be regex, you could always use the Validate filters that are in PHP.
filter_var('http://example.com', FILTER_VALIDATE_URL);
filter_var(mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]]);