Edward Posted April 2, 2007 Share Posted April 2, 2007 Is it possible to sppecify which characters you allow in a variable? For example, only letters and numbers, or only lettes with the hyphen character, or only characters a, b, c, x, y, z, !, - Thank you. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 2, 2007 Share Posted April 2, 2007 yes but all i know is is would use regular expressions.. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 2, 2007 Share Posted April 2, 2007 Yes; see the references in Resources for more information. Are you Unicode-friendly, or do you only define "letters" as A-Z? Quote Link to comment Share on other sites More sharing options...
Edward Posted April 3, 2007 Author Share Posted April 3, 2007 I'm not sure what you mean Effigy, I would want to choose each individual character I want to allow. for example, only b c 1 2 and fullstop. Quote Link to comment Share on other sites More sharing options...
c4onastick Posted April 3, 2007 Share Posted April 3, 2007 I'm not entirely sure I understand your question either; you want to limit the contents of a variable to certain characters? If that's correct, you can just throw them in a character class like so: $test = 'aeiou but sometimes y'; if( preg_match('/^[aeiou]+$/', $test) ){ echo 'Esta bueno!'; // Cheesy attempt at humor, just watched 'Man on Fire' }else{ echo 'No Good!'; } Here's a quick breakdown of what I'm doing here: preg_match('/^[aeiou]+$/', $test) -- Anchors the match to the beginning of the string preg_match('/^[aeiou]+$/', $test) -- Here's your character class, these are the characters you want to allow preg_match('/^[aeiou]+$/', $test) -- This says match one or more of the characters in the preceding class preg_match('/^[aeiou]+$/', $test) -- This is the end of line anchor, basically makes the match have to match from the beginning to the end of the string. Check the links in either effigy's or my signature for some info/tutorials on regex. Hope this helps! Quote Link to comment Share on other sites More sharing options...
c4onastick Posted April 3, 2007 Share Posted April 3, 2007 Out of curiosity effigy, how would you modify this to include some Unicode characters (not A-Z)? (I haven't had to deal with unicode yet, but I'd like to learn how.) Quote Link to comment Share on other sites More sharing options...
effigy Posted April 3, 2007 Share Posted April 3, 2007 There's also locales, which I get tangled up in myself. Typically, for example, \w only matches [a-zA-Z0-9_]. This can change based on the locale, which would (still) only include one language. A broader approach is to use Unicode syntax, and more specifically properties (\p{...} or \P{...}), which are language independent. The ASCII way: [a-zA-Z], which matches literal characters. The Unicode way: \p{L}, which matches characters that have the property of "L", which is short for "Letter." Quote Link to comment Share on other sites More sharing options...
Edward Posted April 3, 2007 Author Share Posted April 3, 2007 Excellent! Thank you! Quote Link to comment Share on other sites More sharing options...
effigy Posted April 3, 2007 Share Posted April 3, 2007 Something I didn't notice the first time: use \z instead of $. See this for more information. Quote Link to comment Share on other sites More sharing options...
c4onastick Posted April 3, 2007 Share Posted April 3, 2007 Ah, I see. I assume that using locales is a much easier way to go. I see your point also, \z can only match at the end of the string whereas $ can match (potentially) at the end of the line as well (using the m modifier). Thanks for the insight effigy! 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.