The Spirit Posted May 6, 2013 Share Posted May 6, 2013 (edited) Im want to validate a URL in my php script and cant quite get it working. Im trying to use if (preg_match($pattern, $link) === false) I have it working in javascript but php is being a problem Heres the Regex (/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)[email protected])?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i); Anyone have a solution? Thanks **edit the regex in this post gets changed by smilies inserting themselves The regex can be seen here http://pastebin.com/4kU801pE Edited May 6, 2013 by The Spirit Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted May 6, 2013 Share Posted May 6, 2013 If your using >= 5.2 you can try filter_var() Quote Link to comment Share on other sites More sharing options...
The Spirit Posted May 6, 2013 Author Share Posted May 6, 2013 filter_var() allows a number of urls I deem not valid for my purpose, thanks though Quote Link to comment Share on other sites More sharing options...
sniko Posted May 6, 2013 Share Posted May 6, 2013 filter_var() allows a number of urls I deem not valid for my purpose, thanks though What kind do you deem valid for purpose, then? Tell us your rule set, and we'll regex that Quote Link to comment Share on other sites More sharing options...
The Spirit Posted May 6, 2013 Author Share Posted May 6, 2013 a, http://a, http://www.a //Not Valid http://localhost //not valid test.com //not valid http://test.com //valid http://www.test.com //valid http://www.test.com?searchblah&blajjj //valid http://www.te$%^$.com //not valid http://test.1223 //Not Valid http://test.co //valid http://test.co.uk //valid http://sub.test.com //valid Quote Link to comment Share on other sites More sharing options...
sniko Posted May 6, 2013 Share Posted May 6, 2013 a, http://a, http://www.a //Not Valid http://localhost //not valid test.com //not valid http://test.com //valid http://www.test.com //valid http://www.test.com?searchblah&blajjj //valid http://www.te$%^$.com //not valid http://test.1223 //Not Valid http://test.co //valid http://test.co.uk //valid http://sub.test.com //valid I'm a little rusty on regex, however, try this :? [http://]+([www.])+[a-zA-Z0-9-]*+.+(a-z)* Quote Link to comment Share on other sites More sharing options...
The Spirit Posted May 6, 2013 Author Share Posted May 6, 2013 Im getting an Unknown modifier '+' error :/ I'd really like to use the same regex as im using for the javascript but it doesnt seem to want to work with php Quote Link to comment Share on other sites More sharing options...
bluegman991 Posted May 6, 2013 Share Posted May 6, 2013 Try this out. Tested and matches all of your valid examples (except http://www.test.com?searchblah&blajjj which should be http://www.test.com/?searchblah&blajjj), and does not match all of your invalid examples. $reg='/^http\:\/\/(?:[a-zA-Z][a-zA-Z0-9]+)(?:\.[a-zA-Z][a-zA-Z0-9]+)*\.(?:com|co\.uk|co)(?:\/.*)?$/'; Quote Link to comment Share on other sites More sharing options...
Djkanna Posted May 6, 2013 Share Posted May 6, 2013 My Regex is also sub par: <?php $urls = array ( 'a', 'http://a', 'www.a', 'test.com', 'http://test.com', 'http://www.test.com', 'http://www.test.com?searchblah&blajjj', 'http://www.te$%^$.com', 'http://test.1223', 'http://test.co', 'http://test.co.uk', 'http://sub.test.com', ); foreach ( $urls as $url ) { echo $url.' --- '. ( preg_match ( '#^http(s)?://(www\.)?([a-z0-9_\-\.]+)\.([a-z\.]){2,5}([/\?&\#\w=]+)?$#', $url ) ? 'Valid' : 'Not Valid' ). '<br />'; } a --- Not Valid http://a --- Not Valid www.a --- Not Valid test.com --- Not Valid http://test.com --- Valid http://www.test.com --- Valid http://www.test.com?searchblah&blajjj --- Valid http://www.te$%^$.com --- Not Valid http://test.1223 --- Not Valid http://test.co --- Valid http://test.co.uk --- Valid http://sub.test.com --- Valid Edit: Edited expression to match '=' also ( for the likes of ?blah=123 ) Quote Link to comment Share on other sites More sharing options...
The Spirit Posted May 6, 2013 Author Share Posted May 6, 2013 (edited) Thanks Djk and Bluegman! Djk Im using yours, the only problem I have is that it allows urls such as http://test Edited May 6, 2013 by The Spirit Quote Link to comment Share on other sites More sharing options...
Djkanna Posted May 7, 2013 Share Posted May 7, 2013 It shouldn't do, or at least in my tests it doesn't. Quote Link to comment Share on other sites More sharing options...
The Spirit Posted May 7, 2013 Author Share Posted May 7, 2013 (edited) Cant seem to get it working :/ This is the code im using: else if ((!preg_match('#^http(s)?://(www\.)?([a-z0-9_\-\.]+)\.([a-z\.]){2,5}([/\?&\#\w=]+)?$#', $url))) { $return['error'] = true; $return['msg'] = 'URL not valid'; } Edited May 7, 2013 by The Spirit Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.