2012-02-05 55 views
0

我想用{用戶名}替換任何twitter @用戶名。我現在有下文中,用戶更換@username:用標籤替換Twitter用戶名

$string = "Tweet with @UserName in the string" 
echo preg_replace('/@([a-z0-9_]+)/i', ' USER ', $tweet); 

// Tweet with USER in the string 

是否有可能修改的preg_replace使得其輸出是

Tweet with {UserName} in the string 

回答

1

嘗試

<?php 
$string = "Tweet with @UserName in the string"; 

echo preg_replace('/\[email protected]([a-z0-9_]+)/i', '{${1}}', $string); 

注:我添加\ B上像[email protected]不會被取代。

+0

難道你不應該逃避{}嗎? – powtac 2012-02-05 20:49:29

+0

它似乎無需逃避它的工作。 – drew010 2012-02-05 20:51:50

+0

重印你的筆記:不應該忽略電子郵件地址?這只是我正在尋找的用戶名:) – Pr0no 2012-02-05 20:53:49