本文介绍了查找和替换@提及的正则表达式,例如twitter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个PHP应用程序,该应用程序需要一个正则表达式来替换诸如twitter之类的@提及.正则表达式还应满足以下需求.

I am developing a PHP application which needs a regular expression to replace the @ mentions like twitter. Also the regular expression should satisfy the following needs.

  1. 如果只有@并且前后没有任何内容,则不应替换.
  2. 电子邮件中的
  3. @不应替换.例如. [email protected]不应替换.
  4. 仅应替换@sam@example之类的字符串,例如<a href="http://twitter.com/sam">@sam</a><a href="http://twitter.com/example">@example</a>
  1. if there is just @ and nothing before and after that then it should not be replaced.
  2. @ in the emails should not be replaced. For eg. [email protected] should not be replaced.
  3. Only strings like @sam or @example should be replaced like <a href="http://twitter.com/sam">@sam</a> and <a href="http://twitter.com/example">@example</a>

请帮助.预先感谢.

推荐答案

哇.我自己找到了答案.

Wow. I found the answer myself guys.

$tweet = preg_replace('/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '$1<a href="http://twitter.com/$2">@$2</a>', $tweet);

感谢您的帮助.

这篇关于查找和替换@提及的正则表达式,例如twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:09