preg_match() probs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mm00re
Forum Newbie
Posts: 13
Joined: Sat Jun 12, 2004 5:56 am

preg_match() probs

Post 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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

where is $val comming from?
mm00re
Forum Newbie
Posts: 13
Joined: Sat Jun 12, 2004 5:56 am

Post 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 = '';
    				  }
           }
Post Reply