views:

14

answers:

1

I am trying do a search to make sure that each of our product titles contains its manufacturer code. I am using preg_match but am getting some weird results back.

Here is a snippet of code I wrote to go through the array of products...

while($row = mysql_fetch_array($result))
        {
                $products[''.$row[0].''][0] = $row[0]; // Product ID
                $products[''.$row[0].''][1] = $row[1]; // MFG Part Number
                $products[''.$row[0].''][2] = $row[2]; // Product Title

                $search_str = $products[''.$row[0].''][1];
                $str_to_search = $products[''.$row[0].''][2];

                if(preg_match("~" . $search_str . "~i" ,$str_to_search)){

Here is what it is returning... (this is in the arrays order)

5300250, P-17(352), Cal June 17" Plastic Buoy Ring P-17(352) NO Match!
5300251, P-21 (352), Cal June 21" Plastic Buoy Ring P-21 (352) NO Match!
5708116, DS-683(12PK), Dr. Shrink Weather Tight Vents 12/Pack DS-683(12PK) NO Match!
5708117, DS-062A(12 PK), Dr. Shrink Hooded Vent 12/Pack DS-062A(12 PK) NO Match!
5802165, 2PB2 (PR), Rod Saver Paddle Bucket 2"x2' Transom Tie Down 2PB2 (PR) NO Match!PHP Warning:  preg_match(): Compilation failed: nothing to repeat at offset 7 in /www/www.boatersplus.com/inventory/append-mfg-code-to-title.php on line 39

5344226, MA7170**, Mustang Survival Re-Arm Kit (For Use With md3003,md3025) MA7170** NO Match!
6503286, 4140(140D), Master Lock Padlock 1 1/2" Brass 4140(140D) NO Match!
6503287, 4150(150D), Master Lock Padlock 1 7/8" Brass 4150(150D) NO Match!
6217549, RBX-123 *NEW*, Solas Hub Kit M/m 1 1/4" RBX-123 *NEW* NO Match!
6503285, 4130(130D), Master Lock Padlock 1 1/8" Brass 4130(130D) NO Match!
6216389, 18-6773 (DROPSHIP), Sierra Trim Motor 18-6773 (DROPSHIP) NO Match!
5802166, 2PB4 (PR), Rod Saver TieDown Pbkl Svr 2"x4' 2PB4 (PR) NO Match!
5802167, 2PB6 (PR), Rod Saver TieDown Ratchet Svr 2PB6 (PR) NO Match!
6217458, RBX-127 *NEW*, Solas Hub Kit Yam F350 RBX-127 *NEW* NO Match!
MM-DD80ALL(R), DD80-A-LL(R), Dana M80 Drive-rebuilt,old Style DD80-A-LL(R) NO Match!

So for some reason these are not maching, it looks to be that the parentheses and asterisks are not matching.

Any help pointing me in the right direction would be greatly appreciated.

Thanks,

Chris Edwards

+1  A: 

You should use the preg_quote() method to escape parentheses (and possible other special chars).

In your case, like this :

preg_quote($search_str, "~")
Colin Hebert
You are a genius! Perfect, Thanks!! Its like an addslash for regex.
Chris Edwards