2012-11-16 30 views

回答

0

如果您想要考慮主題字符串中的換行符,您需要添加以將'm'modifier添加到正則表達式,也許也是's'。

這個片段做什麼你問:

<?php 
$test = <<<END 
@ 
String mystr = "[email protected]" 
String mystr = [email protected]@@@::example.com; 
@ 

END; 

echo preg_replace('/@\s(.*)@\s/sm', "<code>\n$1</code>\n", $test); 
?> 

輸出:

<code> 
String mystr = "[email protected]" 
String mystr = [email protected]@@@::example.com; 
</code> 

如果輸入字符串是超過1雙「@」,那麼正則表達式應該包括'U' 改性劑:

<?php 
$test = <<<END 
@ 
String mystr = "[email protected]" 
String mystr = [email protected]@@@::example.com; 
@ 

@ 
String mystr2 = "[email protected]" 
String mystr2 = [email protected]@@@2::example.com; 
@ 

END; 

echo preg_replace('/@\s(.*)@\s/smU', "<code>\n$1</code>\n", $test); 
?> 

輸出:

<code> 
String mystr = "[email protected]" 
String mystr = [email protected]@@@::example.com; 
</code> 

<code> 
String mystr2 = "[email protected]" 
String mystr2 = [email protected]@@@2::example.com; 
</code> 
+0

非常goo.thanks.but如果我有超過1代碼的字符串,它合併他們在一起! – user1829014

+0

@ user1829014我已更新我的答案。如果我理解正確,第二部分涵蓋你說的話。 – sierrasdetandil

+0

太棒了。很感謝 – user1829014

0

我想說的最簡單的方法就是非正則表達式的方法:

$splitted = str_split($your_string, "@"); 
$first = array_shift($splitted); 
$last = array_pop($splitted); 

$result = "[code]" . implode("@", $splitted) . "[/code]"; 

或者類似的東西,不管你看到最好的。

+0

thanks.i在我的string.and中有另一個東西,正則表達式是最好的方式做。但這種方式是行不通的 – user1829014

相關問題