Wednesday, 15 April 2015

php - How to split a string on comma that is NOT followed by a space? -



php - How to split a string on comma that is NOT followed by a space? -

i want results be:

class="lang-none prettyprint-override">cats, felines & cougars dogs snakes

this closest can get.

$string = "cats, felines & cougars,dogs,snakes"; $result = split(',[^ ]', $string); print_r($result);

which results in

array ( [0] => cats, felines & cougars [1] => ogs [2] => nakes )

the split() function has been deprecated i'm using preg_split instead.

here's want:

$string = "cats, felines & cougars,dogs,snakes"; $result = preg_split('/,(?! )/', $string); print_r($result);

this uses ?! signify want split on comma when not followed grouped sequence.

i linked perl documentation on operator since preg_split uses perl regular expressions:

http://perldoc.perl.org/perlre.html#look-around-assertions

php regex preg-split

No comments:

Post a Comment