preg_replace 只删除@后面的词
preg_replace remove only the word after @
我正在尝试删除字符串中的用户名。尝试了下面的代码,但它删除了@
之后的所有内容
$string = 'he @username die';
$string = preg_replace('/@.*/','',$string);
echo $string; // output: he
我希望输出是:他死了
谢谢
使用 \S
表示任何不是 space 字符(\s
的反义词)而不是 .
:
$string = 'he @username die';
$string = preg_replace('/@\S+/','',$string);
echo $string; // output: he die
您可能还想删除以下 space:
$string = 'he @username die';
$string = preg_replace('/@\S+\s*/','',$string);
echo $string; // output: he die
我正在尝试删除字符串中的用户名。尝试了下面的代码,但它删除了@
之后的所有内容$string = 'he @username die';
$string = preg_replace('/@.*/','',$string);
echo $string; // output: he
我希望输出是:他死了
谢谢
使用 \S
表示任何不是 space 字符(\s
的反义词)而不是 .
:
$string = 'he @username die';
$string = preg_replace('/@\S+/','',$string);
echo $string; // output: he die
您可能还想删除以下 space:
$string = 'he @username die';
$string = preg_replace('/@\S+\s*/','',$string);
echo $string; // output: he die