2015-08-15 85 views
0

我如何替換*裏面的字符()與字符-preg替換裏面的特定字符


例如:

$x="AUDI A4 Avant (8D5* B5)* A4 (8D2* B5)* A6 Avant (4A* C4)* A6 Avant (4B5* C5)* A6 (4A* C4)*A6 (4B2* C5)* AUDI (FAW) A6L Stufenheck (4B5* C5)* VW PASSAT Variant (3B5)* PASSAT (3B2)"; 

$x輸出將返回

$x="AUDI A4 Avant (8D5- B5)*A4 (8D2- B5)*A6 Avant (4A- C4)*A6 Avant (4B5- C5)*A6 (4A- C4)*A6 (4B2- C5)*AUDI (FAW) A6L Stufenheck (4B5- C5)*VW PASSAT Variant (3B5)*PASSAT (3B2)"; 

回答

1

有一試:

查找內容:(\([^*]*)\*([^*)]*\))
替換爲:$1-$2

說明:

(  : begining of group 1 
    \( : a parenthesis 
    [^*]* : 0 or more character that is not an asterisk 
)  : end of group 1 
\*  : an asterisk 
(  : begining of group 2 
    [^*)]*: 0 or more character that is not an asterisk or a parenthesis 
    \) : a parenthesis 
)  : end of group 2