My php code goes like this:
$str = "1. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway 2. What is love 1.1? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway";
echo "<pre>";
print_r(preg_split('/(?=[\d]+\.)/', $str, -1, PREG_SPLIT_NO_EMPTY));
echo "<pre/>";
And the output is:
Array
(
[0] => 1. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
[1] => 2. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
)
I'm trying to solve this problem that I discovered. And the problem is when my $str is like this: $str = "1. What is love 1.1? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
I'm getting:
Array
(
[0] => 1. What is love
[1] => 1.1? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
[2] => 2. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
)
I don't want "1.1" to be on the index[1], I want it to stay on index[0].
I've tried tweaking the pattern which I'm using in preg_split()
but I'm failing on what I want to achieve...
Can anyone give some advice on what should I do?
You can use a negative lookahead to make sure there is no digit following DOT:
print_r(preg_split('/(?=\d+\.(?!\d))/', $str, -1, PREG_SPLIT_NO_EMPTY));
Array
(
[0] => 1. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
[1] => 2. What is love 1.1? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
)