haku Posted March 5, 2008 Share Posted March 5, 2008 Regular expressions - I suck at them. Can someone give me a hand? I need to know how to test a string to make sure its only alphabetic characters, and no letters or other characters. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/ Share on other sites More sharing options...
soycharliente Posted March 5, 2008 Share Posted March 5, 2008 I think I'm right... '^[a-zA-Z]$' There is also a board SOLELY for regex questions. http://www.phpfreaks.com/forums/index.php/board,43.0.html Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-483991 Share on other sites More sharing options...
cry of war Posted March 5, 2008 Share Posted March 5, 2008 ctype_alpha("$value") letters only ctype_digit("$value") numbers only ctype_alnum("$value") letters and numbers only above can only equal true or false $value="hello world"; if (ctype_alpha("$value")==true) {echo "Good Job";} else {echo "WTF?";} output=> Good Job Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-483996 Share on other sites More sharing options...
haku Posted March 5, 2008 Author Share Posted March 5, 2008 Thanks to both of you! I didn't realize there is a regex section of the board. I will have to use that in the future. I need a regex expression. I'm actually not testing for alphabetic characters to tell the truth, I'm testing for Japanese kana, so the three functions in the previous post wont work. But I didn't think anyone here would know how to write regex expressions for Japanese, so I'm just going to substitute from a regex expression. Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-484000 Share on other sites More sharing options...
discomatt Posted March 5, 2008 Share Posted March 5, 2008 '^[A-z]*$' is what you want... if (preg_match('/^[A-z]*$/', $subject)) { // string matched } else { // contains non-alphabet characters } Cry Of War's is better, if you don't mind spaces in the string. Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-484001 Share on other sites More sharing options...
cry of war Posted March 5, 2008 Share Posted March 5, 2008 Wait the words or the Symbols? Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-484018 Share on other sites More sharing options...
effigy Posted March 5, 2008 Share Posted March 5, 2008 I'm testing for Japanese kana I think this will do it: /[\x{3040}-\x{309F}\x{30A0}-\x{30FF}]/u Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-484048 Share on other sites More sharing options...
haku Posted March 5, 2008 Author Share Posted March 5, 2008 Thanks effigy! I'll try that out when I get to work. Thats great, cause I played around with it for quite a while. Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-484414 Share on other sites More sharing options...
haku Posted March 6, 2008 Author Share Posted March 6, 2008 I tried that effigy, but that was for ascii encoding, and I am using EUC-JP, so it didn't match. Im looking on the japanese net now to see what the Japanese folk have to say about this. Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-484581 Share on other sites More sharing options...
haku Posted March 6, 2008 Author Share Posted March 6, 2008 Thanks for the help everyone. I managed to get my code working. I thought it would be as simple as substituting the first and last katakana (one of the Japanese alphabets) into the English regex, but it wasn't. In the end, it was this code that did it for me: if (mb_ereg('[。-゚]', $string)) { echo "string is katakana"; } else { echo "string is not katakana"; } I don't imagine that many people will need this code for anything, but I thought I would post it here in case someone searches for it sometime. Note: This works in EUC-JP encoding. I don't know and doubt that it will work with other encodings (Shift_JIS and utf-8 for example) Quote Link to comment https://forums.phpfreaks.com/topic/94523-test-string-for-range-of-characters/#findComment-484619 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.