douping1993 2017-09-11 02:50
浏览 41
已采纳

PHP codeigniter通过正则表达式将字符串拆分为数组

I have a text file and want to split the text into array using regular expression. But I am new to regex and don't know how to use it. The text file format is basically like this:

0,"20"1,"100000050"25,"100000050"19,""11,"Masuda"12,"Jin"
I want to split them like:
0: 0,"20"
1: 1,"100000050"
2: 25,"100000050"
...

Please help! Any answer would be appreciated!

  • 写回答

2条回答 默认 最新

  • dsqe46004 2017-09-11 03:35
    关注

    Use the preg_split() function. It operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.

    Using PREG_SPLIT_DELIM_CAPTURE returns the parenthesized expression in the delimiter pattern.

    preg_split(
      '/([\d]+,\"[0-9a-zA-Z]+\")/',
      $str,
      -1,
      PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
    );
    

    /([\d]+,\"[0-9a-zA-Z]+\")/ is the regular expression.

    / = start or end of pattern string
    [ ... ] = grouping of characters
    \d - digits
    + = one or more of the preceeding character or group
    , = the literal comma character
    \" = the literal quote character
    [0-9a-zA-Z] = numbers and letters
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功