dongxue9997 2014-04-11 08:52
浏览 30
已采纳

从另一个字符串搜索字符串并将这些字符串添加到数组[关闭]

My string is:

$str = "a quick brown fox over the lazy dog... #fox #dog. hello everybody #lazy";

I want to get #fox, #dog and #lazy from that string and also every words that contains# and I want to add these string to an array like that:

 $array = array(
       [0]=>'#fox',
       [1]=>'#dog',
       [2]=>'#lazy',
   );

Anyone who can help me.. Please. Thanks a lot!

  • 写回答

3条回答 默认 最新

  • douyu0845 2014-04-11 09:01
    关注

    You could make use of this regular expression '/#(\w+)/'

    <?php
    $str = "a quick brown fox over the lazy dog... #fox #dog. hello everybody #lazy";
    preg_match_all('/#(\w+)/', $str, $matches);
    array_walk($matches[1],function (&$v){ $v='#'.$v;});
    print_r($matches[1]);
    

    OUTPUT :

    Array
    (
        [0] => #fox
        [1] => #dog
        [2] => #lazy
    )
    

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算