Jump to content

Recommended Posts

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]
<?php
function Variable($element) {                       //letters, numbers, spaces
  return !preg_match ("/[^A-z0-9,.] /", $element);//commas and periods only
}
?>
[/code]

Thanks,

Ken
[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]
<?php
function 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]
<?php
function 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 author=kenwvs link=topic=100421.msg396289#msg396289 date=1152792928]
Positive versus Negative

Depending 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]
<?php
preg_match('|^[a-z0-9_,. ]+$|i', $String);
?>
[/code]

Negative:
matching any characters [b]that do not fall into your list of allowables[/b]
[code]
<?php
preg_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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.