2011-11-04 77 views
3

這裏是我與替換子是方括號之間用PHP正則表達式

[sitetree_link%20id=2] 

工作的子我需要更換[]之間愛上一個空白的20%,所有出現。但很顯然,如果[]括號外有%20s,請讓它們獨立...

我只是在學習正則表達式,不過這個看起來很難。任何人都有這個超級聰明的正則表達式?

謝謝:)

回答

5

你可以試試這個

$result = preg_replace('/(\[[^]]*?)(%20)([^]]*?\])/m', '$1 $3', $subject); 

說明

(   # Match the regular expression below and capture its match into backreference number 1 
    \[   # Match the character 「[」 literally 
    [^]]  # Match any character that is NOT a 「]」 
     *?   # Between zero and unlimited times, as few times as possible, expanding as needed (lazy) 
) 
(   # Match the regular expression below and capture its match into backreference number 2 
    %20  # Match the characters 「%20」 literally 
) 
(   # Match the regular expression below and capture its match into backreference number 3 
    [^]]  # Match any character that is NOT a 「]」 
     *?   # Between zero and unlimited times, as few times as possible, expanding as needed (lazy) 
    \]   # Match the character 「]」 literally 
) 
+0

很好的解釋! –

+0

啊差不多了!只需要替換一個空的空間,即。 「」而不是「」 非常感謝:) –

+0

@LiveSource請檢查編輯後的版本。 –

相關問題