在字符串中查找@mentions [英] Finding @mentions in string

查看:93
本文介绍了在字符串中查找@mentions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我尝试用锚标记替换所有出现的@mention:

Trying to replace all occurrences of an @mention with an anchor tag, so far I have:

$comment = preg_replace('/@([^@ ])? /', '<a href="/$1">@$1</a> ', $comment);

采用以下示例字符串:

"@name kdfjd fkjd as@name @ lkjlkj @name"

到目前为止,一切都可以,但是我想忽略单个"@"符号.我尝试在"[^ @]"之后使用"+"和"{2,}",我认为这会强制执行最少的匹配项,但无法正常工作.

Everything matches okay so far, but I want to ignore that single "@" symbol. I've tried using "+" and "{2,}" after the "[^@ ]" which I thought would enforce a minimum amount of matches, but it's not working.

推荐答案

替换问号(?)量词(可选"),并在字符类之后添加+(一个或多个") :

Replace the question mark (?) quantifier ("optional") and add in a + ("one or more") after your character class:

@([^@ ]+)

这篇关于在字符串中查找@mentions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆