search
Homephp教程php手册php substr截断汉字乱码解决办法

在php中substr是用来截取字符的,但是有朋友会发现把它来截英文字母是没有任何问题,但是如果截中文时会出现半个汉字乱码了,下面我来介绍一些解决办法.

substr() 函数返回字符串的一部分。

语法:substr(string,start,length)

实例代码如下:

<?php
echo substr("Hello world!", 6);
?>

输出 world!

实例代码如下:echo substr('中国文',1);

结果就是筹码了,后来才知道中文与英文的区别在于内编码了,一个网站这样说到substr函数在截取字符时是按字节来截取的,中文字符在GB2312编码时为2个字节,utf-8编码时为3个字节,所以截取指定长度的字符串时如果截断了汉字,那么返回的结果显示出来便会出现乱码.

解决办法:1、利用mb_substr来截取,代码如下:

<?php
$str = &#39;这样一来我的字符串就不会有乱码^_^&#39;;
echo "mb_substr:" . mb_substr($str, 0, 7, &#39;utf-8&#39;);
//结果:这样一来我的字
echo "<br>";
echo "mb_strcut:" . mb_strcut($str, 0, 6, &#39;utf-8&#39;);
//结果:这样

?>

但是如果要使用mb_substr截取我们需要使用使用mbstring扩展库,如果没有权限的朋友我们就可参考下面函数,代码如下:

<?php
function msubstr($str, $start, $len) {
    $tmpstr = "";
    $strlen = $start + $len;
    for ($i = 0; $i < $strlen; $i++) {
        if (ord(substr($str, $i, 1)) > 0xa0) {
            $tmpstr.= substr($str, $i, 2);
            $i++;
        } else $tmpstr.= substr($str, $i, 1);
    }
    return $tmpstr;
}
?>

            

教程链接:

随意转载~但请保留教程地址★

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),