1

好的,感谢这个论坛上的人们,我终于得到了一个简单的 twitter 在线网站。现在我遇到了另一个问题。

我有一个 twitter 提要说示例文本是:

猫总是在包里@folks @charliesheen

现在我有了使用正则表达式查找文本中所有@_字符串并将它们替换为的想法

<a href="index.php?".$(matched_string - @)>matched_string</a>

关于我如何才能做到这一点的任何想法?

4

1 回答 1

4

这是我快速破解的东西。 \S意思是“任何不是空格的字符”。

$str = 'The cat will always be in the bag @folks @charliesheen';
$str = preg_replace('/@(\S*)/', '<a href="index.php?$1">$1</a>', $str);

编辑:为了安全起见,您应该确保 URL 中的所有字符都是“url-safe”。

$str = 'The cat will always be in the bag @folks @charliesheen';
$str = preg_replace_callback('/@(\S*)/', function($x){
    return '<a href="index.php?'.urlencode($x[1]).'">'.$x[1].'</a>';
}, $str);
于 2012-06-10T06:43:24.143 回答