doudu9148 2014-06-12 16:42
浏览 67
已采纳

如何在php中使用preg_split()?

Can anybody explain to me how to use preg_split() function? I didn't understand the pattern parameter like this "/[\s,]+/".

for example:

I have this subject: is is. and I want the results to be:

array (
  0 => 'is',
  1 => 'is',
)

so it will ignore the space and the full-stop, how I can do that?

  • 写回答

4条回答 默认 最新

  • douzhan1994 2014-06-12 16:50
    关注

    preg means Pcre REGexp", which is kind of redundant, since the "PCRE" means "Perl Compatible Regexp".

    Regexps are a nightmare to the beginner. I still don’t fully understand them and I’ve been working with them for years.

    Basically the example you have there, broken down is:

    "/[\s,]+/"
    
    / = start or end of pattern string
    [ ... ] = grouping of characters
    + = one or more of the preceeding character or group
    \s = Any whitespace character (space, tab).
    , = the literal comma character
    

    So you have a search pattern that is "split on any part of the string that is at least one whitespace character and/or one or more commas".

    Other common characters are:

    . = any single character
    * = any number of the preceeding character or group
    ^ (at start of pattern) = The start of the string
    $ (at end of pattern) = The end of the string
    ^ (inside [...]) = "NOT" the following character
    

    For PHP there is good information in the official documentation.

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

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧