techiefreak05 Posted July 23, 2007 Share Posted July 23, 2007 I have this form that allows (well, its supposed to) users to enter a youtube url and that corresponding video is supposed to who up on their profile. but, i have to make sure the url they enter is a valid youtube url, but the following code i have does not work, even if the url is valid, it doesnt work. <?php if(!preg_match("~http://www.youtube.com/watch?v=\w+~", $url)){ echo "<b>Please Enter a Valid YouTube™ URL</B>"; }else{ // put video on profile... } ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted July 23, 2007 Share Posted July 23, 2007 "?" is a preg metacharacter.. try this if(!preg_match("~http://www.youtube.com/watch\?v=\w+~", $url)){ Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted July 23, 2007 Author Share Posted July 23, 2007 Thanks, but that does not fix it. Quote Link to comment Share on other sites More sharing options...
btherl Posted July 23, 2007 Share Posted July 23, 2007 What url are you testing with? Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted July 23, 2007 Author Share Posted July 23, 2007 $url=$_POST['url']; // so yeah, its just a form field. but after i check if its a real youtube url, then i add security to it. Quote Link to comment Share on other sites More sharing options...
btherl Posted July 23, 2007 Share Posted July 23, 2007 Can you add this line and copy and paste the output (for a url which fails validation): print "<p>The url I am testing is " . htmlspecialchars($url) . "</p>"; Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted July 23, 2007 Share Posted July 23, 2007 I'm not sure of this one... <?php if(!preg_match("/http:\/\/www.youtube.com\/watch?v=.*/", $url)){ echo "<b>Please Enter a Valid YouTube™ URL</B>"; }else{ // put video on profile... } ?> OR <?php if(!preg_match("/http:\/\/www.youtube.com/", $url)){ echo "<b>Please Enter a Valid YouTube™ URL</B>"; }else{ // put video on profile... } ?> Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted July 24, 2007 Author Share Posted July 24, 2007 The output: The url I am testing is http://www.youtube.com/watch?v=kP5rpxEhTsw the output looks like it should be right! but why isnt it? grr. lol. Quote Link to comment Share on other sites More sharing options...
btherl Posted July 24, 2007 Share Posted July 24, 2007 This works for me: $url = 'http://www.youtube.com/watch?v=kP5rpxEhTsw'; if(!preg_match("~http://www.youtube.com/watch\?v=\w+~", $url)){ print "Failure \n"; } else { print "Success :)\n"; } Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted July 24, 2007 Author Share Posted July 24, 2007 that works! thanks a lot! and to everybody who offered help! Quote Link to comment Share on other sites More sharing options...
effigy Posted July 24, 2007 Share Posted July 24, 2007 A . is a metacharacter that matches anything (except a new line, usually). You should use \. to match a literal period. 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.