【发布时间】:2011-11-27 08:54:56
【问题描述】:
我有这个小代码来提取#hashtags:
$text = 'The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. #lorem #ipsum #another#tags';
$content = explode(' ', $text);
$tags = array();
foreach ($content as $item){
if (preg_match('/#([^\s]+)/', $item, $matches)) {
$tags[]= $matches[0];
}
}
得到了这个:
Array(
[0] => #lorem
[1] => #ipsum
[2] => #another#tags
)
问题是:我如何才能匹配#another#tags 并附加到我当前的数组?
新问题:一些文本有像 http://someurl.com/here.html#top 这样的 url,#top 部分也像标签一样被解析。有什么办法可以避免吗?
【问题讨论】:
-
你的意思是你想要四个标签:#lorem、#ipsum、#another 和#tags?
-
Twitter 主题标签中间不能有 #。事实上,他们只能拥有
[a-zA-Z]{1}[a-zA-Z0-9]*。更新您的正则表达式应该可以解决您面临的问题。