<?php
$str = '17:30 Football 18:30 Meal 20:00 Quiet';
$chars = preg_split('/^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r ($chars);
?>
returned:
Array (
[0] => Array (
[0] => 17:30 Football 18:30 Meal 20:00 Quiet
[1] => 0
)
)
while I was hoping for:
Array (
[0] => Array (
[0] => Football
[1] => 7
)
[1] => Array (
[0] => Meal
[1] => 22
)
etc.
What can I do?
You need to drop the anchors
^and$around your regex - with them in place the regex can never match as they require the splitting string to begin at the start of the string and to end at the end of the string - this can never be true unless your input is17:30only.You might also want to include space characters in your regex, like