ereg not behaving as expected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
msaspence
Forum Newbie
Posts: 1
Joined: Tue Mar 27, 2007 4:23 pm

ereg not behaving as expected

Post by msaspence »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm having a problem with the ereg function to verify a user submitted variable

the situation is that the user is submitting a textarea post variable
there are a number of allowed characters
there are more in the actual application but i have removed to demonstrate the problem as removing them makes no difference
the php code is as follows

Code: Select all

$message=$_POST['textarea1'];
if (ereg("^[A-Za-z0-9\n]*$",$message)) {
$textValid=TRUE;
} else {
echo "error";
}
but it seems to act like

Code: Select all

$message=$_POST['textarea1'];
if (ereg("^[A-Za-z0-9]*$",$message)) {
$textValid=TRUE;
} else {
echo "error";
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de2.php.net/manual/en/ref.regex.php wrote:Tip
PHP also supports regular expressions using a Perl-compatible syntax using the PCRE functions. Those functions support non-greedy matching, assertions, conditional subpatterns, and a number of other features not supported by the POSIX-extended regular expression syntax.

Warning
These regular expression functions are not binary-safe. The PCRE functions are.

[...]
See Also
For regular expressions in Perl-compatible syntax have a look at the PCRE functions.
try

Code: Select all

$textValid = preg_match('!^[A-Za-z0-9 \r\n]*$!', $message) > 0;
if( false===$textValid ) {
	echo 'invalid input';
}
Post Reply