Page 1 of 1

preg_match() probs

Posted: Thu Jun 24, 2004 7:34 am
by mm00re
hey to all,

i have a little preg_match problem i need some assistance with if you can ?

i am trying to match the error from a sql query to a form field value so i can highlight the field for the user

code is a s follows:

Code: Select all

// $error is of form
         // ERROR 1062:
         // Duplicate entry 'asdfasd' for key 2
         if(strlen($error) > 0){
              $errString = split(':', $error);
              $errString = split('''', $errString[1]);
              print_r($errString[1] . '<br>');   
              // output here is: asdfasd

              if(preg_match("/\b$val\b/i", $errString[1])){
    				    $style = 'style="color:#FF0000; background-color: #CCCCCC;"';
    				  }
    				  else{    				    
    				    $style = '';
    				  }
           }
now a sample input to this is:

Code: Select all

Company Name:  	6 - A New Company
Product Name: 	   asdfasd
Title:          	  qref adsf345qe qewrf 435tw7dsaasdaser23f
Key Number: 	   ju8577356 365asr43q a35676848sdfwqer
Date: 	                24-06-2004
Time Code: 	    3 56756 3w47846 3w3234rqwe4q34 qerw
Tape Name: 	   3 - Hawaii Five O
Comments:
output of above PHP snippet highlights both the first and last fields, namely; Company Name and Comments, yet im doing a word boundary match and the contents of comments is the null string '' :S

kind regards,

g00fy

Posted: Thu Jun 24, 2004 10:53 am
by Illusionist
where is $val comming from?

Posted: Thu Jun 24, 2004 7:38 pm
by mm00re
here is full listing,

BTW is there a better way to do this or should i stick to this,
im adding var's to $globals using

Code: Select all

extract($_GET, EXTR_SKIP | EXTR_REFS);
BTW $elements is an assoc array mapping form element names to display names for output

Code: Select all

foreach ($elements as $k => $v){
      foreach($GLOBALS as $key => $val){        
        if(($key == $k) && (strlen($val) > 0)){           
            
           if(strlen($error) > 0 && (strpos($error, '//') === false)){
              $errString = split(':', $error);
              $errString = split('''', $errString[1]);
              //print_r($errString[1] . '<br>');              
              
              $value = preg_match("/\b$val\b/i", $errString[1]);
              //echo ' $value = ' . $value;
              if(preg_match("/\b$val\b/i", $errString[1])){
    				    $style = 'style="color:#FF0000; background-color: #CCCCCC;"';
    				  }
    				  else{    				    
    				    $style = '';
    				  }
           }