kenwvs Posted July 13, 2006 Share Posted July 13, 2006 I have written the following function, hoping to achieve that it will only allow letters, numbers, spaces, comma, and period. I don;t want it to allow any special characters like & $ brackets * All the shift number characters basically need to be disallowed.[code]<?phpfunction Variable($element) { //letters, numbers, spaces return !preg_match ("/[^A-z0-9,.] /", $element);//commas and periods only}?>[/code]Thanks,Ken Quote Link to comment https://forums.phpfreaks.com/topic/14474-help-on-a-function-not-sure-if-it-will-work-resolved/ Share on other sites More sharing options...
obsidian Posted July 13, 2006 Share Posted July 13, 2006 [quote author=kenwvs link=topic=100421.msg396284#msg396284 date=1152792317]I have written the following function, hoping to achieve that it will only allow letters, numbers, spaces, comma, and period. I don;t want it to allow any special characters like & $ brackets * All the shift number characters basically need to be disallowed.[code]<?phpfunction Variable($element) { //letters, numbers, spaces return !preg_match ("/[^A-z0-9,.] /", $element);//commas and periods only}?>[/code]Thanks,Ken[/quote]very close, but your space also needs to be within your brackets:[code]<?phpfunction Variable($element) { return preg_match('|^[a-z0-9,. ]+$|i', $element);}?>[/code]the only difference here is that i'm doing a positive match while you were doing the negative. Quote Link to comment https://forums.phpfreaks.com/topic/14474-help-on-a-function-not-sure-if-it-will-work-resolved/#findComment-57263 Share on other sites More sharing options...
kenwvs Posted July 13, 2006 Author Share Posted July 13, 2006 Positive versus NegativeDepending on whether you want it to come back true or false? Quote Link to comment https://forums.phpfreaks.com/topic/14474-help-on-a-function-not-sure-if-it-will-work-resolved/#findComment-57264 Share on other sites More sharing options...
zq29 Posted July 13, 2006 Share Posted July 13, 2006 [quote author=kenwvs link=topic=100421.msg396289#msg396289 date=1152792928]Positive versus NegativeDepending on whether you want it to come back true or false?[/quote]Correct. Quote Link to comment https://forums.phpfreaks.com/topic/14474-help-on-a-function-not-sure-if-it-will-work-resolved/#findComment-57274 Share on other sites More sharing options...
obsidian Posted July 13, 2006 Share Posted July 13, 2006 [quote author=kenwvs link=topic=100421.msg396289#msg396289 date=1152792928]Positive versus NegativeDepending on whether you want it to come back true or false?[/quote]actually, here's what i meant:Positive:matching to make sure that [b]every character in the string matches[/b] what you're after:[code]<?phppreg_match('|^[a-z0-9_,. ]+$|i', $String);?>[/code]Negative:matching any characters [b]that do not fall into your list of allowables[/b][code]<?phppreg_match('|[^a-z0-9_,. ]|i', $String);?>[/code]which in turn determines whether or not you want true or false to come back... for instance, if a string is TRUE for the positive match, it will be FALSE for the negative one, and vice versa. i like to match in such a way that my function itself returns exactly what i'm after. you've essentially done the same thing with your "!" before preg_match() in your original function, but i've just included that in the match itself.hope that clears things up a bit :P Quote Link to comment https://forums.phpfreaks.com/topic/14474-help-on-a-function-not-sure-if-it-will-work-resolved/#findComment-57280 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.