【问题标题】:PHP Regex Match parenthesesPHP 正则表达式匹配括号
【发布时间】:2010-10-19 19:09:38
【问题描述】:

我正在尝试进行搜索以确保我们的每个产品标题都包含其制造商代码。我正在使用 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!

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

任何帮助我指出正确的方向将不胜感激。

谢谢,

克里斯·爱德华兹

【问题讨论】:

    标签: php regex preg-match


    【解决方案1】:

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

    在你的情况下,像这样:

    preg_quote($search_str, "~")
    

    【讨论】:

    • 你是个天才!很好,谢谢!!它就像正则表达式的附加斜线。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2016-02-13
    相关资源
    最近更新 更多