Chinese substr in PHP is garbled?

Guanhui
Release: 2023-03-02 22:02:02
Original
3044 people have browsed it

Chinese substr in PHP is garbled?

#The substr in PHP is garbled in Chinese?

The substr Chinese characters in PHP are garbled. The reason is that the "substr" function splits Chinese characters according to the length of 3, so it will cause garbled characters. The solution is to use the function in the "mbstring" extension. "mb_substr", this function will calculate a Chinese character as length 1.

Usage examples

<?php


header("Content-Type: text/html; charset=utf-8");

$str="php中文网php中文网php中文网php中文网";

echo strlen($str)."<br>";

echo substr($str,0,12)."<br>";

echo mb_strlen($str,"UTF8")."<br>";

echo mb_substr($str,0,12,"UTF8")."<br>";
?>
Copy after login

Recommended tutorial: "PHP"

The above is the detailed content of Chinese substr in PHP is garbled?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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