PHP 中的 Javascript 函数 fromCharCode()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12990195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:34:24  来源:igfitidea点击:

Javascript function in PHP fromCharCode()

phpjavascript

提问by LIGHT

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

// Output: plain

Is there any PHP Code to work as String.fromCharCode()of javascript?

是否有任何 PHP 代码可以作为String.fromCharCode()javascript工作?

采纳答案by techfoobar

Try the chr()function:

试试这个chr()功能:

Returns a one-character string containing the character specified by ascii.

返回一个包含由 ascii 指定的字符的单字符字符串。

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

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

回答by Baba

PHP has chrfunction which would return one-character string containing the character specified by ascii

PHP 有chr函数,它会返回一个包含 ascii 指定字符的字符串

To fit your java script style you can create your own class

为了适应您的 Java 脚本风格,您可以创建自己的类

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

Class Used

使用的类

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

回答by Amitd

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;
    } 

回答by xdazz

The live demo.

现场演示。

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

And you could make a function:

你可以创建一个函数:

function str_fromcharcode() {
    return implode(array_map('chr', func_get_args()));
}

// usage
$output = str_fromcharcode(112, 108, 97, 105, 110);

回答by Niet the Dark Absol

The chr()function does this, however it only takes one character at a time. Since I'm not aware of how to allow a variable number of arguments in PHP, I can only suggest this:

chr()函数执行此操作,但一次只需要一个字符。由于我不知道如何在 PHP 中允许可变数量的参数,我只能建议:

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