I have been trying several ways to solve my issue and have found a poor work-around, but I would like to know if there is something else out there. I have a string of several sub-strings which are separated by commas. I can split this up into an array using preg_split or explode. BUT some of the sub-strings also contain commas that I do not want to split into separate array members. My work around is to include a full stop at the end of every string and then tell explode to split only on ".,". Example string:
$string = "Henry the horse, Billy the donkey, Harry the mule, George, the hippo";
Work-around
$string = "Henry the horse., Billy the donkey., Harry the mule., George, the hippo.";
$list = explode('.,',$string);
I can't for the life of me think of any way to tell the program that the comma after George is not the end of the sub-string. Another (related) issue is that I would like to split the string at the commas BUT include the commas in the array members.
==> Henry the horse,
==> Billy the donkey,
==> Harry the mule,
==> George, the hippo,
My idea for this is simply to add them again after. Is there a simpler way? In other words is there a way of splitting at a delimiter BUT keeping the delimiter in the array members?