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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?