doudu9148 2014-06-12 08: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 08: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部