jumpenjuhosaphat Posted February 7, 2007 Share Posted February 7, 2007 Okay, I am trying to figure out the regex's so I can form my own. I am currently trying to build a regex that will validate a URL. The url can be: http://www.domain.com http://domain.com http://www.domain.com/ http://domain.com/ http://sub.domain.com or without the http, or with https, and accepting any TLD. I need to strip anything after the TLD, so if there are 3 "/"'s, then anything after the third "/" will need to be removed. Also, if the http(s) part isn't included, I would like to add it in. Here is what I've come up with so far: ((http | https):\/\/){1}([a-zA-Z0-9]\.)*([a-zA-Z]+\/?)$ Is that even close? Any help is appreciated, please. Link to comment https://forums.phpfreaks.com/topic/37457-regular-expression-question/ Share on other sites More sharing options...
jumpenjuhosaphat Posted February 7, 2007 Author Share Posted February 7, 2007 Here is my second go at it: ^((http|https|ftp):\/\/){1} ( ([a-zA-Z0-9]([a-zA-Z0-9_-]\.)*) (([a-zA-Z0-9][a-zA-Z0-9_-]*)+ \/?)$ ) Link to comment https://forums.phpfreaks.com/topic/37457-regular-expression-question/#findComment-179095 Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 Some related topics: http://www.phpfreaks.com/forums/index.php/topic,123121.0.html http://www.phpfreaks.com/forums/index.php/topic,96937.0.html <pre> <?php $tests = array( 'http://www.domain.com', 'http://domain.com', 'http://www.domain.com/', 'http://domain.com/', 'http://sub.domain.com' ); foreach ($tests as $test) { echo $test, ' => '; echo preg_match('% ^(?:https?|ftp):// (?:[a-zA-Z0-9]+\.)+ [a-zA-Z0-9]+/? \z %x', $test); echo '<br>'; } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/37457-regular-expression-question/#findComment-179142 Share on other sites More sharing options...
jumpenjuhosaphat Posted February 7, 2007 Author Share Posted February 7, 2007 Okay, so I found a function called parse_url, that someone pointed out should work for what I need. Here is how I used it: elseif($type=="url") { if(!preg_match("/^http(s)?/si", $var)) { $var = "http://" . trim($var); } $var = parse_url( $var ); $var=$var['scheme'] . "://" . $var['host']; } What you don't see in this code is that I trimmed the $var variable and stripped slashes, and added slashes and stripped tags. SHould this work for what I need it for? Link to comment https://forums.phpfreaks.com/topic/37457-regular-expression-question/#findComment-179147 Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 http(s)? does not need the parentheses; use https? SHould this work for what I need it for? The best way to tell is to try it out. Create some test cases like I did, and see if the results are what you want. Link to comment https://forums.phpfreaks.com/topic/37457-regular-expression-question/#findComment-179152 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.