Jump to content


Photo

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


  • Please log in to reply
4 replies to this topic

#1 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 13 July 2006 - 12:05 PM

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.


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

Thanks,

Ken

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 13 July 2006 - 12:11 PM

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.


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

Thanks,

Ken


very close, but your space also needs to be within your brackets:
<?php
function Variable($element) {
  return preg_match('|^[a-z0-9,. ]+$|i', $element);
}
?>

the only difference here is that i'm doing a positive match while you were doing the negative.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 13 July 2006 - 12:15 PM

Positive versus Negative

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

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 13 July 2006 - 12:31 PM

Positive versus Negative

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

Correct.

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 13 July 2006 - 12:42 PM

Positive versus Negative

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


actually, here's what i meant:

Positive:
matching to make sure that every character in the string matches what you're after:
<?php
preg_match('|^[a-z0-9_,. ]+$|i', $String);
?>

Negative:
matching any characters that do not fall into your list of allowables
<?php
preg_match('|[^a-z0-9_,. ]|i', $String);
?>

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
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users