johnsmith153 Posted December 3, 2011 Share Posted December 3, 2011 Hi, I am planning on learning Regular Expressions but just don't have the time right now. I am trying to fix something for a friend and just want a quick fix. Please don't reposed with "have you searched Google?" or anything like that as I have and can't find the answer (within 20 mins anyway). I simply want the Regular Expression for checking for the existence of one single character. (1) Check if an @ sign exists in a string. (2) Check if any number exists in a string. ...both are for separate checks, so one check is to simply check if an @ sign exists in a string, and the other check is the number one. Please help. Thanks. Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 3, 2011 Share Posted December 3, 2011 I think if you just want to match a single charcter you're over complicating it using regex. Try this: <?php $email = 'name@example.com'; if (strstr($email, '@')) { echo "your pattern matched"; } else { echo "Your patten didn't match"; } ?> Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 3, 2011 Author Share Posted December 3, 2011 Ok, great, thanks, although I think strpos would be better. Also, what about the number question, so: (1) "hhdhjdjdhjh1" = yes (2) "ddddd" = no (3) "jjjj333" = yes Check if any number appears anywhere in the string. etc. Thanks again for responding. Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 3, 2011 Share Posted December 3, 2011 Hi mate Ok this is testing my limited regex knowledge to the limits here but i think this works: <?php $pattern2 = '/^(?=.*\d)|(?=.*\d)(?=.*[a-zA-Z]).{1,}$/'; $email = 'uhuuyyv576576576'; if (preg_match($pattern2, $email)) { echo "your pattern matched"; } else { echo "Your patten didn;t match"; } ?> It should match any pattern of letters and number, or just number but it won't match just letters...hehe confusing myself now... Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 3, 2011 Share Posted December 3, 2011 Hi mate Err I was having a fiddle with this and a much more simpler version would be: <?php $pattern5 = '/[\w]?[\d]+/'; $email = '111eeee'; if (preg_match($pattern5, $email)) { echo "your pattern matched"; } else { echo "Your patten didn't match"; } ?> That is a much simpler way of matching alphanumeric characters or just numeric. It won't match just letters though. Sorry looked at regex sooo long ago that i thought it needed to be a lot more complicated than it did! Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 3, 2011 Author Share Posted December 3, 2011 Great, thanks, I thought it looked long just to do a simple check, but it worked so I was happy with that! Thanks again. Also, have you any idea how to remove everything from a string except letters, numbers and the @ sign, so: "hello123-123" = "hello123123" "John o'Leary" = "John oLeary" "aaaaaa11111111_1111@" = "aaaaaa111111111111@" Thanks again. Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 3, 2011 Share Posted December 3, 2011 Hmm I would try something like $string = 'John_Smith fee334343'; // This is the pattern the pre_replace will use looking for matches $patterns = array(); $patterns[0] = '/[ \-_]/'; // The array value here that corresponds to the above pattern will replace matches //with the contents of the array value. So in this case it replaces anything found //in the pattern with '' - i.e. removes spaces, hyphens etc. $replacements = array(); $replacements[0] = ''; echo preg_replace($patterns, $replacements, $string); Great, thanks, I thought it looked long just to do a simple check, but it worked so I was happy with that! Thanks again. Also, have you any idea how to remove everything from a string except letters, numbers and the @ sign, so: "hello123-123" = "hello123123" "John o'Leary" = "John oLeary" "aaaaaa11111111_1111@" = "aaaaaa111111111111@" Thanks again. Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 3, 2011 Share Posted December 3, 2011 Better version $string = 'John_Smith 533r3r3 >>>'; $patterns = array(); $patterns[0] = '/[^\w]/'; $replacements = array(); $replacements[0] = ''; echo preg_replace($patterns, $replacements, $string); This replaces everything except letters, numbers or underscores. Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted December 3, 2011 Author Share Posted December 3, 2011 How would you change it to keep the @ sign and also the full stop? So keep letters, numbers, @ and . but nothing else? Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 3, 2011 Share Posted December 3, 2011 Hi mate Try this as the pattern: $patterns[0] = '/[^\w@.]/'; essentially that pattern means replace everything except what is inside the square brackets. The \w applies too all letters, numbers and the underscore. If you also want to add in a hyphen as an allowed character then use: $patterns[0] = '/[^\w@.\-]/'; Hope that helps matie I have had a good refresher in simple regex today hehe 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.