EchoFool Posted January 28, 2008 Share Posted January 28, 2008 I need some help i need to find a way in php to check if a string contains only numbers or letters or the symbol _ I know how to check if the string is_numeric but don't how to make it check for symbols other than the _ symbol... What function does this, hope you can help. Link to comment https://forums.phpfreaks.com/topic/88252-solved-input-only-allow-certain-format/ Share on other sites More sharing options...
rhodesa Posted January 28, 2008 Share Posted January 28, 2008 $text = "i_am_valid_123"; if(preg_match("/^\w$/",$text)){ //valid }else{ //not valid } Link to comment https://forums.phpfreaks.com/topic/88252-solved-input-only-allow-certain-format/#findComment-451578 Share on other sites More sharing options...
EchoFool Posted January 28, 2008 Author Share Posted January 28, 2008 So how come this line: if(preg_match("/^\w$/",$text)){ has like a random w in it etc could explain what the symbols all do.. like is it check that the symbols are in the string .. if so do i have to add more symbols to it i want less symbols to be "allowed"? Link to comment https://forums.phpfreaks.com/topic/88252-solved-input-only-allow-certain-format/#findComment-451585 Share on other sites More sharing options...
rhodesa Posted January 28, 2008 Share Posted January 28, 2008 First note, I forgot a +...so it should be preg_match("/^\w+$/",$text) the function is pretty basic, you give it a pattern and a string to match against. Read more here The pattern is a regular expression pattern. Here is a reference sheet on all the symbols: http://www.regular-expressions.info/reference.html Breakdown of the one I provided: / => Starts the pattern ^ => Forces the pattern to match from the beginning of the string \w => Special character, or shorthand, for matching a character, number, or underscore + => Means there must be one or more of the previous character (\w) $ => Forces the pattern to match until the end of the string / => Ends the pattern For more Regular Expression help, check out http://www.phpfreaks.com/forums/index.php/board,43.0.html Link to comment https://forums.phpfreaks.com/topic/88252-solved-input-only-allow-certain-format/#findComment-451594 Share on other sites More sharing options...
EchoFool Posted January 28, 2008 Author Share Posted January 28, 2008 so if i wanted it to check for only numbers and letters... it would be: preg_match("/^$/",$text) Also with regards to this: + => Means there must be one or more of the previous character (\w) Does that mean user names "MUST" have an underscore symbol? or have I mis-understood? Link to comment https://forums.phpfreaks.com/topic/88252-solved-input-only-allow-certain-format/#findComment-451598 Share on other sites More sharing options...
rhodesa Posted January 28, 2008 Share Posted January 28, 2008 preg_match("/^$/",$text) That would only match an empty string, because you haven't really specified anything to match. The \w stands for any character OR any number OR an underscore. The + means match one or more of any of those three types. The symbols for Regular Expressions can be difficult to understand at first. The http://www.regular-expressions.info/ site has good tutorials, examples, and reference sheets. Link to comment https://forums.phpfreaks.com/topic/88252-solved-input-only-allow-certain-format/#findComment-451620 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.