Home > Backend Development > PHP Problem > How to convert Chinese characters to ascii in php

How to convert Chinese characters to ascii in php

藏色散人
Release: 2023-03-05 06:36:01
Original
2560 people have browsed it

php method to convert Chinese characters to ascii: first create a php sample file; then use the encapsulated "public function strtoascii($str){}" method to convert Chinese characters to ascii.

How to convert Chinese characters to ascii in php

Recommended: "PHP Video Tutorial"

Use it directly, the function is below, encapsulated by yourself, Personal test is correct

Convert the string (also practical for 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);
    }
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template