1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP - Find certain characters & text within a field

Discussion in 'PHP' started by ian_ok, Aug 30, 2005.

  1. #1
    I'm trying to find certain characters in a field and display an error or remove that sign now I want to allow:
    the @ sign but don't want to allow % , ;

    If I use this:
    elseif (preg_match('/[[:punct:]]/', $_POST['from'])){
    echo "Punct error";
    }
    It replies error for any @ entered but I want the @ symbol to be allowed.

    Also if a field contains say the word bcc I want an error to say the word bcc is not allowed or just to remove it.

    Is this possible?

    Ian
    SEMrush
     
    ian_ok, Aug 30, 2005 IP
    SEMrush
  2. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hmm... while I'm all for using regexes in many situations, why not just keep this simple:

    
    function validate_from_field($s) {
      $forbidden = array('%', ',', ';', 'bcc');
      foreach ($forbidden as $f)
        if (strpos($s, $f) !== false) return false;
      return true;
    }
    ...
    else if (!validate_from_field($_POST['from'])) {
      echo 'Validation failed'; // Crash and burn
    }
    
    PHP:
     
    Willy, Aug 30, 2005 IP
    ian_ok likes this.
  3. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm getting an error as described in the code below...
    $to = 'email@site.com';
    $message = $_POST['message'];
    $full_name = $_POST['full_name'];
    $no_in_party = $_POST['no_in_party'];
    $mail_list = $_POST['mail_list'];
    $welcome_pack = $_POST['welcome_pack'];
    $from_check = $_POST['from_check'];
    $from = $_POST['from'];
    $tel = $_POST['tel'];
    $bcc = $_POST['bcc'];
    $cc = $_POST['cc'];
    clean_from($_POST);
    function clean_from( &$from )
    {
    if(is_array($from)){
    array_walk(&$from,'clean_from');
    return;
    } else {
    $value = str_replace(array("\r","\n","Content-Type:"),"",$from);
    }
    }
    clean_from_check($_POST);
    function clean_from_check( &$from_check )
    {
    if(is_array($from_check)){
    array_walk(&$from_check,'clean_from_check');
    return;
    } else {
    $value = str_replace(array("\r","\n","Content-Type:"),"",$from_check);
    }
    }
    function validate_from_field($s) {
      $forbidden = array('%', ',', ';', 'bcc');
      foreach ($forbidden as $f)
        if (strpos($s, $f) !== false) return false;
      return true;
    }
    //******Parse error: parse error, unexpected T_ELSE On next line ******
    else if (!validate_from_field($_POST['from'])) {
      echo 'Validation failed'; // Crash and burn
    }
    $headers = "From: ". $_POST['from'] ."\r\n\r\n";  
      $message = "Additional Requests:   ".$message;
      $message .= "\nName:   ".$full_name;
      $message .= "\n\nNo in party:   ".$no_in_party;
      $message .= "\nMailing List:   ".$mail_list;
      $message .= "\nWelcome Pack:   ".$welcome_pack;
      $message .= "\nTel:   ".$tel;
      $message .= "\nEmail:   ".$from;
      $message .= "\nEmail check (different?):   ".$from_check;
    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $from)) {
      echo "<h4>Sorry you have entered an invalid email address, please check and try again</h4>";
    } 
    elseif ($full_name == "") {
      echo "<h4>Sorry you have not entered your name</h4>";
    }
    elseif ($from_check <> $from) {
      echo "<h4>Please verify your email address, they are different.</h4>";
    }   
    elseif ($bcc != ''){
    echo "System Error";
    }
    elseif ($cc != ''){
    echo "System error";
    }
    elseif
     (mail($to,$subject,$message,$headers)){
      echo "<h4>$full_name</h4>";
      echo "<h4>Your reservation request has been sent, we will confirm back to you as soon as possible.</h4>";
       }
    else {
      echo "<h4>Sorry we cannot send your email please try again or send an email to email@site.com </h4>";
     echo "<a href='javascript:history.back(1);'>Click here to return</a>";
    }
    
    PHP:
    Ian
     
    ian_ok, Aug 30, 2005 IP
  4. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Remove the '}' on the line just before the function I wrote. It looks like you're closing the 'if' statement there, so when PHP gets to the 'else if' after the function, it doesn't know what to do.
     
    Willy, Aug 30, 2005 IP
  5. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Still get the same error for this line of code:
    else if (!validate_from_field($_POST['from'])) {
    PHP:
    Ian
     
    ian_ok, Aug 30, 2005 IP
  6. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you remove the ELSE from the above it works fine.

    Thanks very much for the code Willy.

    You can add more if (!validate... statements to the code and change the form field value ($_POST['form field value'] and it works fine, remember what you are blocking as you may need a new function to block a different character set.

    Regards Ian
     
    ian_ok, Aug 30, 2005 IP