moola Posted January 18, 2007 Share Posted January 18, 2007 I have a form where users can submit urls.How do I verify a url is submitted in these forms exactly:[code]http://www.youtube.com/v/shgRSgYwBv0[/code]or[code]http://www.youtube.com/watch?v=shgRSgYwBv0[/code]\else Exit the script and prompt the user for a correct url. Quote Link to comment Share on other sites More sharing options...
Orio Posted January 18, 2007 Share Posted January 18, 2007 Altough I am a regex newbie, I think this will do:[code]<?php$regex = "/^http:\/\/www\.youtube\.com\/((v\/[a-zA-Z0-9]+)|(watch\?v=[a-zA-Z0-9]+))$/"if(!preg_match($regex, $url)) die("Invalid URL!");else{ //....}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 18, 2007 Share Posted January 18, 2007 [code]%^http://www\.youtube\.com/(?:v/|watch\?v=)[a-zA-Z0-9]{11}\z%[/code]The 11 is exact based on your examples. This may need to be tuned. Quote Link to comment Share on other sites More sharing options...
moola Posted January 18, 2007 Author Share Posted January 18, 2007 Is there a way to have a variation on the 11 to adjust for less or more characters. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 18, 2007 Share Posted January 18, 2007 Yes. The valid syntax within the[tt] { } [/tt]quantifier is:# for an exact match#, for a minimal match,# for a maximum match#,# for a ranged matchSee the links in my signature for more information. 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.