How to change file encoding to utf8 in php

藏色散人
Release: 2023-03-05 15:08:01
Original
3827 people have browsed it
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);
Copy after login

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!

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