IOS 13 push token in php

I have a problem with push tokens in IOS 13 and higher. As you know, they come in this version in the format {length = 32, bytes = 0xd3d997af 967d1f43 b405374a 13394d2f ... 28f10282 14af515f } There are a bunch of solutions on the internet, but they are all either in swift or c#. I used the example in c#

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    //DeviceToken = Regex.Replace(deviceToken.ToString(), "[^0-9a-zA-Z]+", "");
    //Replace the above line whick worked up to iOS12 with the code below:
    byte[] bytes = deviceToken.ToArray<byte>();
    string[] hexArray = bytes.Select(b => b.ToString("x2")).ToArray();
    DeviceToken = string.Join(string.Empty, hexArray);
}

I wrote an analog in php

private function preparePushToken($pushToken) {
        if (!preg_match('/length/', $pushToken)) {
            return $pushToken;
        }

        $pushToken = explode('=', $pushToken);

        $rest = $pushToken[2];
        $rest = preg_replace('/^0x(.{8,8})(.{8,8})(.{8,8})(.{8,8})(.{3,3})(.{8,8})(.{8,8})(\})$/', '$1 $2 $3 $4 $5 $6 $7', $rest);

        $pushToken = '{length = 32, bytes = ' . $rest . ' }';

        $result = [];
        foreach(str_split($pushToken) as $char){
            array_push($result, sprintf("%02X", ord($char)));
        }
        return implode('', $result);

        return $result;
    }

However, as you can see from the examples, in the c# example, deviceToken comes in an incomprehensible in the NSData format, and in my database, push tokens are stored in rows. Such a question - how to convert a string to NSData in php?

Author: Dmitry Dagadin, 2020-08-26