How to Transcript JavaScript Functions for PHP

4

How can I transcribe these functions to PHP?

function bytesToWords(bytes) {
    var str;
    for(var i = 0; i < bytes.length; i += 2) {
        var char = bytes[i] << 8;
        if (bytes[i + 1])
            char |= bytes[i + 1];
        str += String.fromCharCode(char);
    }
    return str.replace('undefined', '');
}

function bytesFromWords (string) {
    var bytes = [];
    for(var i = 0; i < string.length; i++) {
        var char = string.charCodeAt(i);
        bytes.push(char >>> 8);
        bytes.push(char & 0xFF);
    }
    return bytes;
}

I have doubts about type functions such as fromCharCode , push how to do such transcription?

    
asked by anonymous 21.04.2015 / 22:30

1 answer

8

Follow this:

  • The equivalent of string.length would be strlen($string) to count the number of characters, for unicode compatibility, use:

    $j = preg_match_all('/.{1}/us', $string, $data);
    
  • The equivalent of bytes.length would be count($bytes) to count the number of items in the array
  • The equivalent of bytes.push(char >>> 8) would be $bytes[] = $char >> 8;

  • To concatenate a string to an existing variable in JavaScript we use += , in PHP we use .=
  • The equivalent of string.charCodeAt would be ord(substr($string, $i, 1)); , for compatibility with unicode, use:

    function unicode_ord($char) {
        list (, $ord) = unpack('N', mb_convert_encoding($char, 'UCS-4BE', 'UTF-8'));
        return $ord;
    }
    
  • The equivalent of String.fromCharCode(char); would be chr($char) , for compatibility with unicode, use:

    function unicode_chr($u) {
        return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
    }
    

I think the code should look like this:

<?php
function unicode_ord($char) {
    list (, $ord) = unpack('N', mb_convert_encoding($char, 'UCS-4BE', 'UTF-8'));
    return $ord;
}

function unicode_chr($u) {
    return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
}

function bytesToWords($bytes) {
    $str = '';
    $j = count($bytes);

    for($i = 0; $i < $j; $i += 2) {
        $char = $bytes[$i] << 8;
        if ($bytes[$i + 1]) {
            $char |= $bytes[$i + 1];
        }
        $str .= unicode_chr($char);
    }
    return $str;
}

function bytesFromWords($string) {
    $bytes = array();
    $j = preg_match_all('/.{1}/us', $string, $data);
    $data = $data[0];

    for($i = 0; $i < $j; $i++) {
        $char = unicode_ord($data[$i]);
        $bytes[] = $char >> 8;
        $bytes[] = $char & 0xFF;
    }
    return $bytes;
}


$data = bytesFromWords('㬁愃膘ƘჀ䚐⦀飠噋&ӡ๨㏃棱쌌ص䌠');

echo implode(', ', $data);
echo bytesToWords($data);

Extra

In the JavaScript version you used str.replace('undefined', ''); because the variable was not set, but it is best to set a value for variable and so you do not need to replace, like this:

function bytesToWords(bytes) {
    var str = "";//Setado string vazia
    for(var i = 0; i < bytes.length; i += 2) {
        var char = bytes[i] << 8;
        if (bytes[i + 1])
            char |= bytes[i + 1];
        str += String.fromCharCode(char);
    }
    return str;
}
    
21.04.2015 / 22:40