Connecting Tech Pros Worldwide Forums | Help | Site Map

Value of 2 fields in 1 preg match

imarkdesigns's Avatar
Member
 
Join Date: Jul 2007
Location: Philippines
Posts: 37
#1: Dec 13 '08
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...

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $value1 = $_POST['value1'];
  4. $value2 = $_POST['value2'];
  5.  
  6. //Post Value in Text Field for Value 1
  7. $text1 = /(Text1, Word1)/i";
  8. $text2 = /(Text2, Word2)/i";
  9.  
  10. //Post Value in Text Field for Value 2
  11. $text3 = /(Text3, Word3)/i";
  12. $text4 = /(Text4, Word4)/i";
  13.  
  14.  
  15. if (preg_match( $text1, $value1 )) {
  16.     $mail->AddAddress('webmail1@localhost.com'); 
  17. } elseif (preg_match( $text2, $value1 )) {
  18.     $mail->AddAddress('webmail2@localhost.com'); 
  19. } else {
  20.     $mail->AddAddress('webmail@localhost.com'); 
  21. }
  22.  
  23. ?>
  24.  
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's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#2: Dec 13 '08

re: Value of 2 fields in 1 preg match


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.
Expand|Select|Wrap|Line Numbers
  1. $search = array($text1, $text2, $text3, $text4);
  2. $values = array($value1, value1, $value2, $value2);
  3. $result = preg_replace($search, '', $values,);
  4. // test for NULL (error in preg_replace())
  5. $length = count($result);
  6. for ($i=0; $i<$length; $i++)
  7. {
  8. // test for empty $values too
  9. // if your replace char is not occuring in $values you may test for its occurence in $result (strpos())
  10.     if ($values[i] != $result[i])
  11.     {
  12.         $mail->AddAddress('webmail2@localhost.com');
  13.     }
  14. }
imarkdesigns's Avatar
Member
 
Join Date: Jul 2007
Location: Philippines
Posts: 37
#3: Dec 14 '08

re: Value of 2 fields in 1 preg match


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.com...

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $value1 = $_POST['value1'];
  4. $value2 = $_POST['value2'];
  5.  
  6. //Post Value in Text Field for Value 1 that will match the input of $value1
  7. $text1 = /(Text1, Word1)/i";
  8. $text2 = /(Text2, Word2)/i";
  9.  
  10. //Post Value in Text Field for Value 2 that will match the input of $value2
  11. $text3 = /(Text3, Word3)/i";
  12. $text4 = /(Text4, Word4)/i";
  13.  
  14. $text_value = array ($value1, $value2);
  15.  
  16. if (preg_match( $text1, $text_value )) {
  17.     $mail->AddAddress('webmail1@localhost.com'); 
  18. } elseif (preg_match( $text2, $text_value )) {
  19.     $mail->AddAddress('webmail2@localhost.com'); 
  20. } else {
  21.     $mail->AddAddress('webmail@localhost.com'); 
  22. }
  23.  
  24. ?>
  25.  
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.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#4: Dec 14 '08

re: Value of 2 fields in 1 preg match


you could use a 3rd array, which is storing the appropriate adresses.
Expand|Select|Wrap|Line Numbers
  1. $default = true;
  2. for ($i=0; $i<$length; $i++)
  3.     if ($values[i] != $result[i])
  4.     {
  5.         $mail->AddAddress($mail[i]);
  6.         $default = false;
  7.         break; // loop is quit after first match
  8.     }
  9. }
  10. if ($default)
  11. {
  12.     $mail->AddAddress('webmail@localhost.com');
  13. }
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
imarkdesigns's Avatar
Member
 
Join Date: Jul 2007
Location: Philippines
Posts: 37
#5: Dec 14 '08

re: Value of 2 fields in 1 preg match


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