ereg not behaving as expected

Discussion in 'PHP' started by msaspence, Mar 27, 2007.

  1. #1
    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

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

    but it seems to act like

    $message=$_POST['textarea1'];
    if (ereg("^[A-Za-z0-9]*$",$message)) {
    $textValid=TRUE;
    } else {
    echo "error";
    }
     
    msaspence, Mar 27, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    First off, I'd use preg_match(), as it is faster.

    The try adding \r to your expression.

    
    
    return preg_match("^[A-Za-z0-9\r\n]*$", $message)) {
    
    PHP:
     
    nico_swd, Mar 27, 2007 IP