cry of war Posted December 7, 2007 Share Posted December 7, 2007 Hello im kind of new to regex i know the very very basic basics like [0-9] and [a-z] but i have a few if strings that i need a regex for so i can See if the whole value or string is only numbers or only letters Numbers 1235415=>true 1244p123=>false .12235135=>false and so on Letters aldkfjalsdf=>true adflkjadfl1=>false .aldkfjasldf=>false and so on if (preg_match($match,$string)) { $x="2"; } any help with this problem(lack of knowledge) would be greatly appreciated Link to comment https://forums.phpfreaks.com/topic/80629-new-to-regex/ Share on other sites More sharing options...
Daniel0 Posted December 7, 2007 Share Posted December 7, 2007 I don't understand the problem. You haven't explained a problem but rather what [0-9] and [a-z] mean. Link to comment https://forums.phpfreaks.com/topic/80629-new-to-regex/#findComment-408932 Share on other sites More sharing options...
effigy Posted December 7, 2007 Share Posted December 7, 2007 Only digits: /\A\d+\z/ Only letters (ASCII): /\A[a-z]+\z/i Link to comment https://forums.phpfreaks.com/topic/80629-new-to-regex/#findComment-409181 Share on other sites More sharing options...
jcd Posted December 7, 2007 Share Posted December 7, 2007 Are you looking for 1 regex which will allow both? If so this will do it I think: $subject = 'asdasdadrg'; echo preg_match('%\A(\d)?(?(1)\d*|[A-Za-z]*)\z%', $subject) ? 'TRUE' : 'FALSE'; Link to comment https://forums.phpfreaks.com/topic/80629-new-to-regex/#findComment-409255 Share on other sites More sharing options...
jcd Posted December 7, 2007 Share Posted December 7, 2007 I've just realised the method above is a silly way of doing it (im new at this...). If you do want to combine them better to do: echo preg_match('%\A(\d+|[A-Za-z]+)\z%', $subject) ? 'TRUE' : 'FALSE'; Link to comment https://forums.phpfreaks.com/topic/80629-new-to-regex/#findComment-409358 Share on other sites More sharing options...
cry of war Posted December 10, 2007 Author Share Posted December 10, 2007 I was looking for a regex that tells you that the whole string is or is not only letters or is or isnt only letters 1235415=>true (all numbers) 1244p123=>false (not all numbers because of "p") .12235135=>false (not all numbers because of ".") and so on Letters aldkfjalsdf=>true(all letters) adflkjadfl1=>false(not all letters because of "1") .aldkfjasldf=>false(not all letters because of ".") would i use preg_match_all will it check to see if the whole string only has only letters or only numbers?? Link to comment https://forums.phpfreaks.com/topic/80629-new-to-regex/#findComment-410999 Share on other sites More sharing options...
Daniel0 Posted December 10, 2007 Share Posted December 10, 2007 ^[0-9]+$ and ^[a-zA-Z]+$ can do that, but why not just use functions like ctype_alpha() to determine if it only consists of alphabetic characters (a-z uppercase and lowercase) and ctype_digit() to determine if it only consists of digits (0-9) (is_numeric() won't work as it will accept floats as well)? Link to comment https://forums.phpfreaks.com/topic/80629-new-to-regex/#findComment-411015 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.