Php 将以@开头的单词替换为twitter链接

Php 将以@开头的单词替换为twitter链接,php,twitter,preg-replace,Php,Twitter,Preg Replace,我需要将文本中以@开头的所有单词替换为指向该twitter帐户的相应链接。现在我用的是这样的东西: $tweet_text = preg_replace('/(@\w+)/', '<a href=\'#\'>\1</a>', $string); 更换后: 'Imperfection is the new perfection... RT @xHausOfCandy: @katyperry i think your bottom teeth and your wonk e

我需要将文本中以@开头的所有单词替换为指向该twitter帐户的相应链接。现在我用的是这样的东西:

$tweet_text = preg_replace('/(@\w+)/', '<a href=\'#\'>\1</a>', $string);
更换后:

'Imperfection is the new perfection... RT @xHausOfCandy: @katyperry i think your bottom teeth and your wonk eye make you even more adorable.'
'Imperfection is the new perfection... RT <a href=''#''>@xHausOfCandy</a>: <a href=''#''>@katyperry</a> i think your bottom teeth and your wonk eye make you even more adorable.'
不完美是新的完美。。。RT:我觉得你的下牙和你那呆板的眼睛让你更可爱 期望的:

'Imperfection is the new perfection... RT <a href=''http://twitter.com/xHausOfCandy''>@xHausOfCandy</a>: <a href=''http://twitter.com/katyperry''>@katyperry</a> i think your bottom teeth and your wonk eye make you even more adorable.'
不完美是新的完美。。。RT:我觉得你的下牙和你那呆板的眼睛让你更可爱
希望现在更清楚了

您可以使用库

您可以使用库

在它自己的捕获组中有名称,并在替换中引用它时使用\2

$tweet_text = preg_replace('/(@(\w+))/', '<a href="http://twitter.com/\2">\1</a>', $string);
$tweet\u text=preg\u replace('/(@(\w+))/,'','$string);

在其自己的捕获组中使用该名称,并在替换中引用它时使用\2

$tweet_text = preg_replace('/(@(\w+))/', '<a href="http://twitter.com/\2">\1</a>', $string);
$tweet\u text=preg\u replace('/(@(\w+))/,'','$string);
$string='不完美是新的完美。。。RT@xHausOfCandy:@katyperry我认为你的下牙和你的书呆子眼睛让你更可爱。”;
$tweet_text=preg_replace('/@(\w+/),'$string);
$string='不完美是新的完美。。。RT@xHausOfCandy:@katyperry我认为你的下牙和你的书呆子眼睛让你更可爱。”;
$tweet_text=preg_replace('/@(\w+/),'$string);


你能发布一个“之前”和“之后”示例来演示你在做什么吗?替换后$tweet_文本的实际输出是什么?他不知道用什么来代替#…这是个问题。请看下面的答案:你能发布一个“之前”和“之后”示例来演示你在做什么吗?实际输出是什么更换后的$tweet_文本?他不知道用什么来代替#…这是个问题。请看下面的答案:你能提供一个例子吗?我没有完全听懂。自从我从手机公司来,我花了一段时间。看一看:-)@luqitaCould只需使用带有
'/@(\w+/“
though@Esailija但是链接中不会包含@符号。我更喜欢尽可能保持动态。如果出于某种原因,我想将@更改为&,我只需要在一个地方进行更改。你的论点是正确的。你能举个例子吗?我没有完全听懂。自从我从手机公司来,我花了一段时间。看一看:-)@luqitaCould只需使用带有
'/@(\w+/“
though@Esailija但是链接中不会包含@符号。我更喜欢尽可能保持动态。如果出于某种原因,我想将@更改为&,我只需要在一个地方进行更改。不过你的论点是正确的。