Jump to content

Archived

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

gfX

eregi help

Recommended Posts

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

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?

Share this post


Link to post
Share on other sites

[code]

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

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.

Share this post


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

Share this post


Link to post
Share on other sites
[!--quoteo(post=386293:date=Jun 21 2006, 01:47 AM:name=gfX)--][div class=\'quotetop\']QUOTE(gfX @ Jun 21 2006, 01:47 AM) [snapback]386293[/snapback][/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:
[code]
<?php
if (preg_match('|[\'"\!@#\$%\^&\*\(\):;\{\}\[\]<>]|', $string)) {
  echo "Invalid character present!";
}
?>
[/code]

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

hope this helps!

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.