Jump to content


Photo

eregi help


  • Please log in to reply
3 replies to this topic

#1 gfX

gfX
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 21 June 2006 - 05:47 AM

Here is what I have:
if(eregi("'", $_POST['fn'])) {
        $error[] = "<strong class=\"error\">You entered an invalid character.</strong>";
        $show = 'y';
    }

How would I make it so I can have a bunch of characters listed in that one line. Like I want ' " ! @ # $% ^ & * ( ) : ; {} [ ] > < etc. to have an error if entered. right now it only gives the error for the symbol '

it always messes up if I try to add the rest in there, with commas or whatever. I dont know the formula its supposed to go in

anyone know?

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 June 2006 - 08:55 AM



if (preg_match('/\'|"|\!|@|#|\$|%|\^|&|\*|\(|)|:|;|\{|}|\[]|>|</', $_POST['fn']))
{        $error[] = "<strong class=\"error\">You entered an invalid character.</strong>";
        $show = 'y';
}

It woudl look something like that - someone will come up with a more elegant solution I am sure oh and correct my regular expression too - just woke up so not firing on cylinders yet.
follow me on twitter @PHPsycho

#3 gfX

gfX
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 21 June 2006 - 11:42 AM

Thanks for your reply, However for some reason it is not working. And it is allowing me to add folders with those characters in it.

And when I don't type anything and I just hit enter - This is what I get:

Warning: preg_match(): Compilation failed: unmatched parentheses at offset 27 in /**/**/gallery.php on line 166
1. You must enter a folder name.

Edit:
Nevermind I Ijust took those parenthesis out and it works fine. Thanks so much

#4 obsidian

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

Posted 21 June 2006 - 12:03 PM

[!--quoteo(post=386293:date=Jun 21 2006, 01:47 AM:name=gfX)--][div class=\'quotetop\']QUOTE(gfX @ Jun 21 2006, 01:47 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
How would I make it so I can have a bunch of characters listed in that one line. Like I want ' " ! @ # $% ^ & * ( ) : ; {} [ ] > < etc. to have an error if entered. right now it only gives the error for the symbol '
[/quote]

first off, i'm going to move this thread over to the regex forum so you'll get more help.

second, try the following... if you create a character family, you can list them all together and let it match any of them:
<?php
if (preg_match('|[\'"\!@#\$%\^&\*\(\):;\{\}\[\]<>]|', $string)) {
  echo "Invalid character present!";
}
?>

now, it sounds like you may be better off to match for correct characters rather than incorrect. for instance, if i want to allow only letters, numbers and underscores, i simply have to do the following:
<?php
if (!preg_match('|^[a-z0-9_]+$|i', $string)) {
  echo "Invalid character present!";
}
?>

hope this helps!
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