正则表达式不包含空格



我正在使用一些atwho.js来允许使用@链接到用户配置文件。

例如@User1 @User2 @User3,所有这些都将链接到他们的配置文件。

我从regexr.com得到正确的反馈,但它没有翻译,

不包括空格

下面是我的记录在数据库中显示的内容:

<p><a href=/profiles/Richard Skiles>@Richard Skiles</a></p>

输出如下:

<p><a href=/profiles/Richard>@Richard Skiles</a></p>
下面是一些代码:
public function setBodyAttribute($body)
{
$find = ['/@([w- ]+)/'];
$replace = ['<a href=/profiles/$1>$0</a>'];
$this->attributes['body'] = preg_replace($find, $replace, $body);
}

希望有人能在这里帮助我。这将是非常感激,谢谢你!

我看到你在字符集中添加了一个空格,但是preg_replace不匹配这样的空格。他们使用s

public function setBodyAttribute($body)
{
$find = ['/@([w-s]+)/']; // I added the s to match the spaces
$replace = ['<a href=/profiles/$1>$0</a>'];
$this->attributes['body'] = preg_replace($find, $replace, $body);
}

最新更新