如何将以哈希标签(#)开头的单词从字符串提取到数组中 [英] How do I extract words starting with a hash tag (#) from a string into an array

查看:160
本文介绍了如何将以哈希标签(#)开头的单词从字符串提取到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含哈希标签的字符串,我试图拉出标签,我想我已经很近了,但是得到了一个具有相同结果的多维数组

I have a string that has hash tags in it and I'm trying to pull the tags out I think i'm pretty close but getting a multi-dimensional array with the same results

  $string = "this is #a string with #some sweet #hash tags";

     preg_match_all('/(?!\b)(#\w+\b)/',$string,$matches);

     print_r($matches);

产生

 Array ( 
    [0] => Array ( 
        [0] => "#a" 
        [1] => "#some"
        [2] => "#hash" 
    ) 
    [1] => Array ( 
        [0] => "#a"
        [1] => "#some"
        [2] => "#hash"
    )
)

我只想要一个数组,每个单词以井号标签开头.

I just want one array with each word beginning with a hash tag.

推荐答案

这可以通过/(?<!\w)#\w+/ regx完成,

this can be done by the /(?<!\w)#\w+/ regx it will work

这篇关于如何将以哈希标签(#)开头的单词从字符串提取到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆