检查字符串中以字符@开头的单词

Hi there and thank you for taking the time to look into this.

I'm working on a comment system and instead of using nested comments, I just want to add the @user method for replies.

So let's say for example I enter this comment:

@moonwalker: Thanks again for your reply.

I'm looking for a function to check if a word is starting with the character @ and after that changing the word into a link. The problem I'm facing is that people will use things like:

@moonwalker: rest of the message

@moonwalker, rest of the message

@moonwalker rest of the message

or even use en email address in their comments.

Is there a simple way to achieve this? Preg_match perhaps?

Thanks in advance for your help.

Try this function:

preg_replace('/(@\w+)/', '<a href=\'#\'>\1</a>', $comment);

You can use preg_match. I haven't tested it, but i suppose it would be something like :

preg_match("/^\@/", $message)

However, i strongly advice you to reconsider the protocol. It's much better to add non printable characters and more than 1 byte. For instance, a stub like "0x1 0x12 0x03 0x07".