Last active
May 20, 2016 10:56
-
-
Save ganey/f26756a1ab928bc882f4734222ee94c6 to your computer and use it in GitHub Desktop.
Domain / url regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// tested here: https://regex101.com/r/iC1uQ4/1 | |
// as laravel validation rule: 'regex:/^((http|https)\:\/\/|)([\w]+\.)+\w+($|[\w\/\.]+$|[\w\/\.]+\?[\w\=\&\.]+$)/' | |
$re = "/^((http|https)\:\/\/|)([\w|\-]+\.)+\w+($|[\w\-\/\.]+$|[\w\/\.]+\?[\w\=\&\.]+$)/"; | |
$str = "www.example.com/metropolitanlondon"; | |
preg_match($re, $str, $matches); | |
/* | |
true www.example.com/metropolitanlondon | |
true www.example.com | |
true http://example.co.uk | |
true example.com//dwa/../dwd | |
true www.example.com/dawdwad/fsef/rdgdr_szg/gsedg?btfbftb=fgersg&gdsrzgrg=rsths.sfra | |
false xrftjcomjfcgyvhj | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment