Use PHP for splitting on spaces and brackets

后端 未结 5 856
醉梦人生
醉梦人生 2021-01-21 09:03

I have this string in $s:

ben say (#yellow) hey

At the moment I\'m using:

$parts = array_filter(preg_sp         


        
5条回答
  •  失恋的感觉
    2021-01-21 09:49

    A one liner which will work exactly as wanted (you don't even need array_filter):

    $s = "ben say (ewfs) as";
    $parts = preg_split('/\s+|(\()|(\))/', $s, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)
    
    /*
    Array(
      0 => "ben",
      1 => "say",
      2 => "(",
      3 => "ewfs",
      4 => ")",
      5 => "as",
    )
    */
    

提交回复
热议问题