2012-11-16 29 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

相关问题