I have a string in this form
$navigation="<li>A</li><li>B</li><li>C</li><li>A</li><li>B</li><li>C</li><li>A</li><li>B</li><li>C</li>";
The list is generated dynamically from database and hence the number of elements is not fixed. I want to split this list after every 5th element, so if there are 10 "<li></li>"
elements in this string I should get two strings output1 and output2.
I have tried explode but it doesn't work as we have to impose restriction on nth element and I have also tried string_split but that is not working also.
So what is the solution?
Ahmar
You can do it this way:
$s = "<li>A</li><li>B</li><li>C</li><li>A</li><li>B</li><li>C</li><li>A</li><li>B</li><li>C</li>";
if (preg_match_all('~((?:<li>.*?</li>){5})~i', $s, $arr))
print_r($arr);
OUTPUT:
Array
(
[0] => Array
(
[0] => <li>A</li><li>B</li><li>C</li><li>A</li><li>B</li>
)
[1] => Array
(
[0] => <li>A</li><li>B</li><li>C</li><li>A</li><li>B</li>
)
)