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... } ?> Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/ 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)){ Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-305075 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. Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-305125 Share on other sites More sharing options...
btherl Posted July 23, 2007 Share Posted July 23, 2007 What url are you testing with? Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-305150 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. Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-305155 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>"; Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-305192 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... } ?> Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-305207 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. Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-305989 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"; } Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-305993 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! Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-306028 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. Link to comment https://forums.phpfreaks.com/topic/61303-solved-help-with-preg_match/#findComment-306300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.