我刚刚了解了PHP中的函数preg_replace()
,但我还不知道如何充分使用它。我的网站上有需要更新的短信,我认为preg_replace()
可以胜任这项工作。
我需要替换类似的文本:
@kev need to replace the @ symbol at the end of this paragraph, @example...
收件人:
@kev need to replace the @ symbol at the end of this paragraph, example...
基本上:
@example...
收件人:
example...
我几乎没有时间进一步研究这个函数,所以我真的需要问一下。谢谢
这应该有效:
$str = '@kev need to replace the @ symbol at the end of this paragraph, @example...';
echo preg_replace('~@(['w]*?)'.'.'.~u', '$1...', $str);
输出:
@kev need to replace the @ symbol at the end of this paragraph, example...
非常硬编码,但有效的解决方案:
$string = "@kev need to replace the @ symbol at the end of this paragraph, @example..." ;
preg_match_all("/'s+@([a-zA-Z0-9]+)/", $string, $matches) ;
$new = preg_replace("/@{$matches[1][0]}/", "{$matches[1][0]}", $string);
echo $new ;