Javascript function in PHP fromCharCode ()

var test = String.fromCharCode(112, 108, 97, 105, 110); document.write(test); // Output: plain 

Is there any php code to work like String.fromCharCode() javascript?

+6
source share
5 answers

Try the chr() function:

Returns a one-character string containing the character specified in ASCII.

http://php.net/manual/en/function.chr.php

+4
source

PHP has a chr function that returns a single character string containing the character specified by ascii

To fit the style of java script, you can create your own class

 $string = String::fromCharCode(112, 108, 97, 105, 110); print($string); 

Used class

 class String { public static function fromCharCode() { return array_reduce(func_get_args(),function($a,$b){$a.=chr($b);return $a;}); } } 
+3
source

Live demo.

 $output = implode(array_map('chr', array(112, 108, 97, 105, 110))); 

And you can make a function:

 function str_fromcharcode() { return implode(array_map('chr', func_get_args())); } // usage $output = str_fromcharcode(112, 108, 97, 105, 110); 
+1
source

Try something like this.

  // usage: echo fromCharCode(72, 69, 76, 76, 79) function fromCharCode(){ $output = ''; $chars = func_get_args(); foreach($chars as $char){ $output .= chr((int) $char); } return $output; } 
+1
source

The chr() function does this, however, it only accepts one character at a time. Since I do not know how to allow a variable number of arguments in PHP, I can only suggest the following:

 function chrs($codes) { $ret = ""; foreach($codes as $c) $ret .= chr($c); return $ret; } // to call: chrs(Array(112,108,97,105,110)); 
0
source

Source: https://habr.com/ru/post/928183/


All Articles