php - Keep delimiter when splitting string -


my code below not keep delimiter when splitting strings using preg_split.

$feature_description = "- 1.read/write speed performance based on internal testing.- 2.tbw (terabytes written) values calculated capacity.";   preg_split('/(- [0-9].)/',$feature_description,null,preg_split_delim_capture | preg_split_no_empty); 

now output was:

    [0] => - 1.     [1] => read/write speed performance based on internal testing.     [2] => - 2.     [3] => tbw (terabytes written) values calculated capacity. 

but want output as:

    [1] => - 1.read/write speed performance based on internal testing.     [2] => - 2.tbw (terabytes written) values calculated capacity. 

rather splitting should doing match using preg_match_all using lookahead based regex:

-\h+\d+.+?(?=-\h+\d+|\z) 

regex demo

regex breakup:

  • -\h+\d+: match hyphen followed 1+ horizontal whitespaces , 1+ digits
  • .+?: match 0 or more of character (lazy)
  • (?=-\h+\d+|\z): lookahead assert have hyphen followed 1+ horizontal whitespaces , 1+ digits or end of string

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -