标签中的字符串替换

时间:2015-01-22 18:28:45

标签: php string preg-replace str-replace

这是我从数组字段获取的字符串:

[email_content] => 

Dear {NAME},

{USR}Thank You For Connecting With "Company" .Your Username is {$username}.{/USR}



{PWD}Thank You For Connecting With "Company".Please Click on the below Link to Resest Yor Password.{/PWD}
 {$password_link}

Thanks & Regards
ABCD Team.

有时我只想省略整个字符串的{USR}和{/ USR}标记,并将其中的内容保持不变,有时我想要替换{USR}和{/ USR}中的整个内容,包括打开和关闭{USR}标签,保持其他字符串内容不变。

我尝试了很多字符串函数,但它只是让我的编码很冗长。

1 个答案:

答案 0 :(得分:3)

怎么样:

$replacement = '$0';
if ($remove_tag_only) {
    $replacement = '$1';
} else if ($remove_all) {
    $replacement = '';
}
$string = preg_replace('~\{USR\}(.+?)\{/USR\}~s', $replacement, $string);