Skip to content
Advertisement

Subdivide a variable by byte length

I have a byte-only variable (generated by openssl_random_pseudo_bytes) and I need to get the first 5 bytes.

I would like to know if this is possible in PHP and, if so, which function should I use?

If you can also provide an example it is appreciated but not required.

Thanks, Have a nice day!

Advertisement

Answer

Use substr()

https://www.php.net/manual/en/function.substr.php

It’s binary safe..

$some_bytes = openssl_random_pseudo_bytes(16);
$first_5 = substr($some_bytes, 0, 5);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement