从字符串中获取所有主题标签[重复]

问题描述 投票:0回答:2

我有这个小代码来提取

#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
部分也像标签一样被解析。有什么办法可以避免吗?

php regex text-extraction hashtag
2个回答
4
投票

如果您想要做的只是取出所有哈希标签,那么您可以简单地执行以下操作:

$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];

0
投票

正则表达式1

/#([^(\s#)]+)/

正则表达式2

/#([^\s^#)]+)/

© www.soinside.com 2019 - 2024. All rights reserved.