2016-05-02 17 views
-1

如何在preg_match_all中創建正則表達式「和」?如何在preg_match_all中創建正則表達式「和」

The mouse to nibble the cork from the bottle russia king. 

$n = preg_match_all("/cork&bottle/i", mb_strtolower($y['foo'], 'UTF-8'), $matches); 

/cork&bottle/i不工作

+0

評估是否需要按照指定順序進行,還是可以按任何順序進行? – joncloud

回答

0

這是我使用的方法:

$string = 'The mouse to nibble the cork from the bottle russia king.'; 
preg_match_all("/^(?=.*(cork))(?=.*(bottle))/i", $string, $matches); 
print_r($matches); 

輸出:

Array 
(
    [0] => Array 
     (
      [0] => 
     ) 

    [1] => Array 
     (
      [0] => cork 
     ) 

    [2] => Array 
     (
      [0] => bottle 
     ) 

) 

說明:

/     : regex delimiter 
    ^    : begining of line 
    (?=    : start lookahead 
     .*   : 0 or more any character 
     (\bcork\b) : capture group #1 that contains "cork" with word boundaries 
    )    : end of look ahead 
    (?=    : start lookahead 
     .*   : 0 or more any character 
     (\bottle\b) : capture group #2 that contains "bottle" with word boundaries 
    )    : end of look ahead 
/i     : regex delimiter, case insensitive