Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);

Take the following sample string:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
279 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

@([^@ ]+)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...