Use PHP for splitting on spaces and brackets

后端 未结 5 869
醉梦人生
醉梦人生 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-21 09:33

    Well you can try this replacement:

    $s = str_replace(array('(', ')'), array('( ', ' )'), $s);
    $parts = array_filter(preg_split('/\s+/', $s));
    

    The trick here is to add a space between your ( and the word so that it gets splitted. However, it will only work specific to your example. Things like (( might cause some unwanted results. If so you can try using preg_replace instead.

提交回复
热议问题