dongpu6141 2014-04-17 12:30
浏览 17
已采纳

使用preg_match PHP分隔每个字母表

i know that this is a beginner level question, but i am stuck here and i need help.

i want to store each alphabet in an array. (Only Alphabets not integers)

let me show you the previous code that i am using

$str = "ab c45 d123ef";
preg_match_all('/./us', $str, $ar);
echo '<pre>';
print_r($ar);

its output

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] =>  
            [3] => c
            [4] => 4
            [5] => 5
            [6] =>  
            [7] => d
            [8] => 1
            [9] => 2
            [10] => 3
            [11] => e
            [12] => f
        )

)

But it also separate integers... what i have to change in the preg_match expression, i want this output.

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] =>  
            [3] => c
            [4] => 45
            [5] =>  
            [6] => d
            [7] => 123
            [8] => e
            [9] => f
        )

)
  • 写回答

2条回答 默认 最新

  • douken1726 2014-04-17 12:34
    关注
    preg_match_all('/[\d.]+|./us', $str, $ar);
    

    [\d.] matches a digit or decimal point, the + quantifier after it matches a sequence of them.

    Result:

    Array
    (
        [0] => Array
            (
                [0] => a
                [1] => b
                [2] =>  
                [3] => c
                [4] => 45
                [5] =>  
                [6] => d
                [7] => 123
                [8] => e
                [9] => f
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料