我有这个小代码来提取
#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
部分也像标签一样被解析。有什么办法可以避免吗?
如果您想要做的只是取出所有哈希标签,那么您可以简单地执行以下操作:
$text = 'The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. #lorem #ipsum #another#tags';
preg_match_all("/\#\\w+/", $text, $matches);
$tags = $matches[0];
正则表达式1
/#([^(\s#)]+)/
正则表达式2
/#([^\s^#)]+)/