Jump to content

Archived

This topic is now archived and is closed to further replies.

kenwvs

Help on a Function - Not sure if it will work RESOLVED

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

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
Positive versus Negative

Depending on whether you want it to come back true or false?

Share this post


Link to post
Share on other sites
[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]Correct.

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites

×

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.