php preg_split() and regex pattern

I’m trying to convert a header list to an array in php using the preg_split() function. The list is constructed as key : value. I lack knowledge in regex.

date: Fri, 22 Nov 2024 10:16:24 GMT
server: Apache
cache-control: no-cache, private
api-version: 1

etc.

$pattern = "/[:]\s/";
$components = preg_split($pattern, $headers);

Gives me this :

Array
(
    [0] => HTTP/2 200 
date
    [1] => Fri, 22 Nov 2024 10:21:14 GMT
server
    [2] => Apache
cache-control
    [3] => no-cache, private
api-version
    [4] => 1
)

which is wrong.

Thanks for your help.

Read more here: Source link