Value of 2 fields in 1 preg match

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imarkdesigns
    New Member
    • Jul 2007
    • 46

    Value of 2 fields in 1 preg match

    Good day to all masters here.. good to be back here again and nice site!

    ok, i have a problem and still confusing to accomplish using 2 values in preg match. here is my sample code...

    Code:
    <?php
    
    $value1 = $_POST['value1'];
    $value2 = $_POST['value2'];
    
    //Post Value in Text Field for Value 1
    $text1 = /(Text1, Word1)/i";
    $text2 = /(Text2, Word2)/i";
    
    //Post Value in Text Field for Value 2
    $text3 = /(Text3, Word3)/i";
    $text4 = /(Text4, Word4)/i";
    
    
    if (preg_match( $text1, $value1 )) {
    	$mail->AddAddress('webmail1@localhost.com'); 
    } elseif (preg_match( $text2, $value1 )) {
    	$mail->AddAddress('webmail2@localhost.com'); 
    } else {
    	$mail->AddAddress('webmail@localhost.com'); 
    }
    
    ?>
    Ok, now the problem here is i don't know where can place or even the better code to place the $value2 inside the preg_match.

    or i will need to create another set of preg_match for $value2?

    i'm open for your all helps and suggestions.. appreciation to all your help.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it might not be the best (or even a good) solution, but it's the only one I can think of:

    preg_replace() works also with arrays, so you could put all your search terms in one array and the test terms in another. use any string for replace (choose wisely). if preg_replace() found (and replaced) your search terms, initial and final string must differ.
    Code:
    $search = array($text1, $text2, $text3, $text4);
    $values = array($value1, value1, $value2, $value2);
    $result = preg_replace($search, '', $values,);
    // test for NULL (error in preg_replace())
    $length = count($result);
    for ($i=0; $i<$length; $i++)
    {
    // test for empty $values too
    // if your replace char is not occuring in $values you may test for its occurence in $result (strpos())
        if ($values[i] != $result[i])
        {
            $mail->AddAddress('webmail2@localhost.com');
        }
    }

    Comment

    • imarkdesigns
      New Member
      • Jul 2007
      • 46

      #3
      Thanks sir Dormilich,

      but the cause of my problem is using sending email from 3 to 5 different email addresses... which is the value of text field for State will match each of dedicated (let say 4 email) address with different state location.

      so let say if the user input Florida as the value of State in text field then the email match will be the webmail1@email. com... and if Chicago, email match will be webmail2@email. com and so on to the 4 emails. and if so the input do not match the 4 emails input of States.. it will deliver to the default value email webmail@email.c om...

      and to the point of the 2nd value is, if the 2nd text field which is County of State will also do the same thing...

      Code:
      <?php
       
      $value1 = $_POST['value1'];
      $value2 = $_POST['value2'];
       
      //Post Value in Text Field for Value 1 that will match the input of $value1
      $text1 = /(Text1, Word1)/i";
      $text2 = /(Text2, Word2)/i";
       
      //Post Value in Text Field for Value 2 that will match the input of $value2
      $text3 = /(Text3, Word3)/i";
      $text4 = /(Text4, Word4)/i";
      
      $text_value = array ($value1, $value2);
      
      if (preg_match( $text1, $text_value )) {
          $mail->AddAddress('webmail1@localhost.com'); 
      } elseif (preg_match( $text2, $text_value )) {
          $mail->AddAddress('webmail2@localhost.com'); 
      } else {
          $mail->AddAddress('webmail@localhost.com'); 
      }
        
      ?>
      i did a minor revision on the code.. and not sure if this is correct.

      i'm open for your all helps and suggestions.. appreciation to all your help.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you could use a 3rd array, which is storing the appropriate adresses.
        Code:
        $default = true;
        for ($i=0; $i<$length; $i++)
            if ($values[i] != $result[i])
            {
                $mail->AddAddress($mail[i]);
                $default = false;
                break; // loop is quit after first match
            }
        }
        if ($default)
        {
            $mail->AddAddress('webmail@localhost.com');
        }
        though I have to admit, that if you have only 4 addresses, it is easier to go for the if-elseif-else construction. my code will probably pay off, if you have a lot of inputs to check.

        regards

        Comment

        • imarkdesigns
          New Member
          • Jul 2007
          • 46

          #5
          ah i see... ok i will try this code and check what else i can code too... many big thanks master!

          Comment

          Working...