Re: Inane ereg problem

From: Date: Tue, 25 Jul 2000 11:04:33 +0000
Subject: Re: Inane ereg problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-8005@lists.php.net to get a copy of this message
> My problem is this...to prevent users from running a bad search the > below code checks for illegal characters, if it finds them...there is an > error message with a display of illegal chars. The problem is the right > square bracket. If I insert \] into the $excludeChars variable the > rountine ceases to work...no error messages, it just won't find any > errors in the sample text. Can someone explain why the right bracket is > causing so much trouble. > ken > $excludeChars="\[\*\.\-\+\{\}\%\\$\|\#\@"; > $text="when in t[he course of human events"; > if(ereg("([$excludeChars])",$text)){ > Print("bad characters\n"); > $letters=implode(" ",explode("\\",$excludeChars)); > print($letters); > > } > else{ > Print("ok to use"); > } If you want to allow just alphanumerics, digits and whitespace and exclude everything else, then you could use strspn() instead. Like this: if (strspn(strtolower($str), "abcdefghijklmnopqrstuvwxyz 0123456789") < strlen($str)) { print(”Illigal characters in string”); } else { print(”String OK”); } Best regards, Jesper Juhl juhl@eisenstein.dk

« previous php.general (#8005) next »