Home  >  Article  >  Backend Development  >  How to convert php characters to ascii?

How to convert php characters to ascii?

coldplay.xixi
coldplay.xixiOriginal
2020-07-16 14:38:583107browse

How to convert characters to ascii in php: first define two integer variables a and A, and save the ASCII codes of the corresponding letters; then define the character variable ch, and assign the capital letter A to the integer variable A; Finally, convert A to ASCII code.

How to convert php characters to ascii?

How to convert characters to ascii in php:

Convert a string (also practical in Chinese) to ascii

Note: I default that our current php file environment is UTF-8. If it is GBK, the mb_convert_encoding operation is not required

public function strtoascii($str){
        $str=mb_convert_encoding($str,'GB2312');
        $change_after='';
        for($i=0;$i<strlen($str);$i++){
            $temp_str=dechex(ord($str[$i]));
            $change_after.=$temp_str[1].$temp_str[0];
        }
        return strtoupper($change_after);
    }

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to convert php characters to ascii?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn