takn25 Posted February 16, 2011 Share Posted February 16, 2011 Hi this is my code I am just understanding Regex kinda a new at this. I have a string which is Tom I want to check that there is nothing other than Alphabets used I made the Pattern variable and used both in the preg_match function. The problem I am facing personally just looking at the code I think the echo should be Alphabets are used but for some reason I am getting echo Contains something other than Alphabets. I haven't been able to figure out what am i doing wrong some help would be appreciated. $pattern = "[^A-Za-z$]"; $tom="Tom"; if (preg_match($pattern, $tom)) {echo "Alphabets are used";} else {echo "Contains something other than Alphabets";} Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted February 16, 2011 Share Posted February 16, 2011 Change pattern to $pattern = "/^[A-Za-z]/"; Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 16, 2011 Share Posted February 16, 2011 Your IF logic is backwards. Your regex is close. It needed some delimiters. Plus you should remove the $. Besides that it was looking to see if there are any NON alpha characters. However, that is how you want to do the validation. It is easier to look for any instance of a non-alpha character than to ensure all the characters are alpha characters. You just need to change the IF/ELSE logic. $pattern = "#[^A-Z]#i"; $tom="Tod4sdadasm"; if (preg_match($pattern, $tom)) { echo "One or more non-alpha characters were found"; } else { echo "All the characters where alpha"; } Also modified the regex by adding the "i" parameter to make the test case insensitive. Change pattern to $pattern = "/^[A-Za-z]/"; Um, no. That expression only tests to see if the FIRST character is a non a-z character. Quote Link to comment Share on other sites More sharing options...
takn25 Posted February 16, 2011 Author Share Posted February 16, 2011 Hi thanks both of you for helping. mjdamato i just read your post that my if else logic is backwards could you please tell me what is the proper way to achieve my desired result. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 17, 2011 Share Posted February 17, 2011 Hi thanks both of you for helping. mjdamato i just read your post that my if else logic is backwards could you please tell me what is the proper way to achieve my desired result. Didn't I post the code already? Let me state again. You Regex had some minor problems but the way it was written it was supposedly checking to see if there were any NON-ALPHA characters. So, the statements in the IF and ELSE conditions were backwards. IF there was a match, then you want the message that there were non-alpha characters, ELSE the string only contained alpha characters. As, I also stated that is exactly how you want to do that validation - look for any character OTHER THAN alpha characters rather than ensure that all the characters are alpha. It is basically the same check, but programatically it is different. The code I provided should do what you want. Quote Link to comment Share on other sites More sharing options...
takn25 Posted February 17, 2011 Author Share Posted February 17, 2011 Yes its working cheers! my friend. 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.