2013-04-01 24 views
0

爲什麼目標字符串中的尖括號會否定第二個示例中的匹配文本。帶尖括號的意外預浸行爲

$str = '[example1](example2)'; 
echo preg_replace('/\[(.*?)\]\((.*)\)/', '$2 $1', $str); 

// output: example2 example1 

$str = '[example1](<example2>)'; 
echo preg_replace('/\[(.*?)\]\((.*)\)/', '$2 $1', $str); 

// output: example1 

回答

2

<example2>被瀏覽器解析爲標籤,所以不會顯示出來。如果你右鍵單擊並查看源代碼,你會在那裏看到它。同樣,如果你通過CLI運行這個,你會看到正確的結果。

+0

omg!該睡了 ;) – esryl

0
$str = '[example1](<example2>)'; 


echo $str; 

會產生[example1]()你,因爲Web瀏覽器假定<example2>是HTML標記。 同樣發生,而顯示結果如下。這場比賽恰好很好。

echo preg_replace('/\[(.*?)\]\((.*)\)/', '$2 $1', $str);