English 中文(简体)
PHP Regex匹配括号
原标题:PHP Regex Match parentheses

我正在尝试进行搜索,以确保我们的每个产品标题都包含其制造商代码。我正在使用preg_match,但得到了一些奇怪的结果。

以下是我为浏览产品阵列而编写的代码片段。。。

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)){

以下是它返回的内容。。。(按数组顺序排列)

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!

因此,由于某种原因,这些不匹配,看起来是括号和星号不匹配。

如果能为我指明正确的方向,我们将不胜感激。

谢谢

克里斯·爱德华兹

最佳回答

您应该使用preg_quote()方法来转义括号(以及可能的其他特殊字符)。

在您的情况下,如下所示:

preg_quote($search_str, "~")
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签