Splitting Input Containing Substrings with the Delimiter
Explore how to split input strings that contain delimiters within quoted substrings by manually iterating over characters. Understand how to handle delimiter exceptions inside quoted text, including escaped characters, to produce correctly parsed arrays in Laravel applications.
We'll cover the following...
We'll cover the following...
Suppose we wanted to break the following string into an array on the , character:
one, two, three
We can accomplish this easily using the explode helper method like so:
This results in the following array:
array:3 [
0 => "one"
1 => " two"
2 => " three"
]
The results are what we would expect given our sample input, but let’s say we want to split the following string:
one, "two, three", four, five
Into this array:
array:5 [
0 => "one"
1 => " two"
2 => " "three, four, five""
3 => " six"
4 => " seven"
]
Using the explode method on this string produces the following results, which are not quite what we are after:
array:5 [
0 => "one"
1 => " "two"
2 => " three""
3 => " four"
4 => " five"
]
If ...