Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding @mentions in string

Tags:

regex

php

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.

like image 766
Rob Avatar asked Apr 30 '12 12:04

Rob


1 Answers

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

@([^@ ]+)
like image 97
Tim Cooper Avatar answered Oct 16 '22 03:10

Tim Cooper