2012-07-21 44 views
2

HOWTO比較字符串:比較路徑字符串通配符在PHP

commodity/search/oil/branch/index 

與此:

commodity/search/*/branch/index 

雖然 「油」 是與其他字代替它應該返回true。

+0

一些正則表達式呢? – mauris 2012-07-21 16:00:33

+0

試試用/替換逗號http://stackoverflow.com/questions/9692914/difference-between-two-strings – Cups 2012-07-21 16:42:42

回答

5
$match = preg_match("/commodity\/search\/(.*)\/branch\/index/", "commodity/search/someotherword/branch/index"); 

$match將是真實的(或一些值評估爲真,如1)如果發現匹配。

注:以上將匹配任何額外的路徑上,如commodity/search/some/other/word/branch/index

如果你只是想一個單詞,而不是類似的路徑結構,那麼你可以嘗試這樣的事:

$match = preg_match("/commodity\/search\/[A-Za-z0-9_-]+\/branch\/index/", "commodity/search/some-OTHER_word/branch/index"); 

這隻會匹配大寫和小寫az字符,數字,連字符和下劃線。根據需要調整。

+1

或者它可以是''/ commodity \/search \/[^ \ /:\\;] + \/branch \/index /「'。 – mauris 2012-07-21 16:13:08

+0

如何動態構建模式? – wajatimur 2012-07-21 16:24:20