Home >Backend Development >PHP Problem >How to change file encoding to utf8 in php

How to change file encoding to utf8 in php

藏色散人
藏色散人Original
2020-09-01 10:42:313886browse
How to change the file encoding to utf8 in php: first read the file into a string through the [file_get_contents] function; then convert the encoding of the string through the iconv method; and finally write the string into the file.

How to change file encoding to utf8 in php

Recommended: "PHP Video Tutorial"

php Convert File Encoding

    $contents_before = file_get_contents($input);
    $contents_after = iconv('UCS-2','UTF-8',$contents_before);
    file_put_contents($input, $contents_after);

Be careful after processing, The file $input has been changed.

The file_get_contents() function reads the entire file into a string.

iconv — The string is converted according to the required character encoding; the

file_put_contents() function writes a string to the file.

The above is the detailed content of How to change file encoding to utf8 in php. 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