stupot2212 Posted December 8, 2009 Share Posted December 8, 2009 I know how simple this must be But I have spent almost 5 hours now -.- If it makes any difference, its for use within WHMCS custom fields validation.. All that I want, is to use a regular expression to check that my input field is : A single word - no spaces and only lower case A to Z letters.. no spaces, no -'s no nothing I am either really stupid or too tired. Would appreciate any help very much Quote Link to comment https://forums.phpfreaks.com/topic/184438-60-second-basic-problem/ Share on other sites More sharing options...
cags Posted December 8, 2009 Share Posted December 8, 2009 if(preg_match("#^[a-z]+$#D", $input)) { echo "Valid"; } Quote Link to comment https://forums.phpfreaks.com/topic/184438-60-second-basic-problem/#findComment-973608 Share on other sites More sharing options...
stupot2212 Posted December 8, 2009 Author Share Posted December 8, 2009 Thankyou very much! #^[a-z]+$#D I understand this much of it : ^[a-z]+$ I had actually tried that before If anyone care's to explain the # and #D it would be beneficial for me and others Thanks again man! Quote Link to comment https://forums.phpfreaks.com/topic/184438-60-second-basic-problem/#findComment-973622 Share on other sites More sharing options...
salathe Posted December 8, 2009 Share Posted December 8, 2009 The # are delimiters, they delimit (mark the start and end points of) the regular expression contained within. They are required because in the PCRE family of functions (preg_*) the regular expression is combined with some (optional) modifiers which affect the behaviour of the expression. The delimiters, regular expression and modifiers combined are called a pattern. Given the pattern /hello/i then the parts are: Delimiter is / Regular expression is hello Modifier is i (makes the expression match case-insensitively) The delimiters are most commonly one of /, ~, # or @. Far more characters are allowed as delimiters but it is not important at this stage to know precisely which ones. The D modifier in cags' pattern affects the behaviour of the $ character in the regular expression. Normally (i.e., without the D modifier; lets ignore multiline mode and character classes for now) the $ matches the point right at the end of the subject string, or immediately before a newline at the end of the subject string if there is one. To give an example: /def$/ matches the string "abcdef" and also "abcdef\n". When the D modifier is used, the second example (with a trailing newline) will not be matched. It is often used just to be that extra little bit sure that the subject string does not contain a trailing newline. An alternative, not wanting to confuse you, is to use the \z escape sequence which will only ever match at the very end of the subject string: /def\z/ will match "abcdef" but not "abcdef\n" Quote Link to comment https://forums.phpfreaks.com/topic/184438-60-second-basic-problem/#findComment-973729 Share on other sites More sharing options...
newbtophp Posted December 8, 2009 Share Posted December 8, 2009 You may also find this useful: http://www.phpguru.org/downloads/PCRE%20Cheat%20Sheet/PHP%20PCRE%20Cheat%20Sheet.pdf Quote Link to comment https://forums.phpfreaks.com/topic/184438-60-second-basic-problem/#findComment-973732 Share on other sites More sharing options...
salathe Posted December 8, 2009 Share Posted December 8, 2009 You may also find this useful: … Interestingly, that cheat sheet completely forgets to mention the D modifier. Quote Link to comment https://forums.phpfreaks.com/topic/184438-60-second-basic-problem/#findComment-973760 Share on other sites More sharing options...
milliclark Posted December 9, 2009 Share Posted December 9, 2009 I think you have got the solution of your problem so nothing to say.... Quote Link to comment https://forums.phpfreaks.com/topic/184438-60-second-basic-problem/#findComment-973972 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.