Hello I have the below java code:
Base64.encodeToString(("users name").getBytes(), 2);
And I'm trying to convert it to php:
bytesEnc("users name");
function bytesEnc($string) {
$bytes = array();
for($i = 0; $i < strlen($string); $i++){
$bytes[] = ord($string[$i]);
}
return $bytes;
}
I converted getBytes()
but I don't know what to do next. I know function base64_encode
exists inn PHP but the second argument of encodetoString
java method with value 2 bothers me. Please, help.
According to the docs the second parameter is a bitwise flag.
If the value is 2, then the flag NO_WRAP
is used:
Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).
This means its removes the new lines.
function bytesEnc($string){
$string = str_replace(PHP_EOL, '', $string);
$bytes = array();
for($i = 0; $i < strlen($string) - 1; $i++){
$bytes[] = ord($string[$i]);
}
return $bytes;
}
echo base64_encode(implode('',bytesEnc('users name')));