donglian1982 2011-11-26 22:57
浏览 24
已采纳

PHP正则表达式字符串

I have this little code to extract the #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];
    }
}

And got this:

Array(
    [0] => #lorem
    [1] => #ipsum
    [2] => #another#tags
)

The question is: How I can match even the #another#tags and append to my current array?

New trouble: some text has urls like http://someurl.com/here.html#top and the #top part is parsed like a tag too. Is there any way to avoid that?

  • 写回答

2条回答 默认 最新

  • duannaxin9975 2011-11-26 23:04
    关注

    If what you're trying to do is just pull out all of the hash tags, then you can simply do this:

    $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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效