Bman900 Posted July 4, 2010 Share Posted July 4, 2010 Am trying to make sure that a URL ends with .png or .jpg with this code: if((strrchr($avatar, '.') !== '.jpg') || (strrchr($avatar, '.') !== '.png')){ echo "That format is not valid, please select another image that is .jpg, .jpeg, .png, or .gif!"; } else { RUN QUERY } I also tried doing this but this doesn't work either: if(!strrchr($img, '.') == '.jpg'){ echo "That format is not valid, please select another image that is .jpg, .jpeg, .png or .gif!"; } elseif(!strrchr($img, '.') == '.jpeg'){ echo "That format is not valid, please select another image that is .jpg, .jpeg, .png or .gif!"; }elseif(!strrchr($img, '.') == '.png') { echo "That format is not valid, please select another image that is .jpg, .jpeg, .png or .gif!"; }elseif(!strrchr($img, '.') == '.gif'){ echo "That format is not valid, please select another image that is .jpg, .jpeg, .png or .gif!"; } } //register else { RUN CODE } No matter what I always get the error message but if I just do one check like this it works: if(strrchr($avatar, '.') !== '.jpg'){ echo "That format is not valid, please select another image that is .jpg, .jpeg, .png, or .gif!"; } else { RUN QUERY } Quote Link to comment https://forums.phpfreaks.com/topic/206707-if-statement-problem/ Share on other sites More sharing options...
kenrbnsn Posted July 4, 2010 Share Posted July 4, 2010 What does the URL look like? Ken Quote Link to comment https://forums.phpfreaks.com/topic/206707-if-statement-problem/#findComment-1081040 Share on other sites More sharing options...
xt3mp0r~ Posted July 4, 2010 Share Posted July 4, 2010 <?php $url = "http://www.test.com/hello.png"; if(preg_match("/^https?:\/\/(?:[a-z\-]+\.)+[a-z]{2,6}(?:\/[^\/#?]+)+\.(?:jpg|gif|png)$/",$url)) echo "Valid"; else echo "invalid"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/206707-if-statement-problem/#findComment-1081044 Share on other sites More sharing options...
Bman900 Posted July 4, 2010 Author Share Posted July 4, 2010 Thank you that works just fine. I just have to learn what you did there though. Quote Link to comment https://forums.phpfreaks.com/topic/206707-if-statement-problem/#findComment-1081049 Share on other sites More sharing options...
xt3mp0r~ Posted July 4, 2010 Share Posted July 4, 2010 I just used preg_match along with an if condition to Perform a regular expression match. That nifty string within preg_match is the regex. Google for more. Quote Link to comment https://forums.phpfreaks.com/topic/206707-if-statement-problem/#findComment-1081056 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.