search
HomeBackend DevelopmentPHP Tutorialphp 不同编码下的字符串长度区分_PHP

UTF-8的中文字符串是三个字节
复制代码 代码如下:
//编码UTF-8
echo strlen('测试文字a测试文字');
echo '-';
echo mb_strlen('测试文字a测试文字','utf-8');
?>
输出:25-9

GB2312的中文字符串是二个字节
复制代码 代码如下:
//编码GB2312
echo strlen('测试文字a测试文字');
echo '-';
echo mb_strlen('测试文字a测试文字','Gb2312');
?>

输出:17-9
在Mysql数据库(5.1以后的版本)中,如果字段类型为varchar(10)则可插入10个字符(不是字节);
所以在判断字符串的长度时需要根据文档编码来区分。
符一个简单的UTF-8下字符串截取(按字符个数截取)
复制代码 代码如下:

/*
* UTF-8字符串截取
* $str 要截取的字串
* $start 截取起始位置
* $length 截取长度
*/
function cutStr($str,$start,$length) {
$restr = '';
$j = 0;
$end = $length + $start - 1;
$plen = strlen($str);
for($i=0;$i$restr .= ord($str[$i])>127 ? $str[$i].$str[++$i].$str[++$i] : $str[$i];
$j++;
if ($j if ($j >= $end){break;}
}
$restr .='';
return $restr;
}
$str = '中新网9月24日电 二十国集团(G20)领导人第三次金融峰会今日将在美国匹兹堡召开。';
echo $str;
echo '
';
echo utf8_substr($str,0,25);
echo '
';
?>
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
java字符串长度是什么java字符串长度是什么Jul 10, 2023 pm 01:29 PM

java字符串长度是指一个字符对象中字符的数量。在java中,每个字符都有一个Unicode值,而java字符串是由Unicode字符组成的序列。因此,java字符串长度的计算方式就是该字符串对象中Unicode字符的数量。java字符串是java语言中最常用的数据类型之一,可以用来存储文本数据。

Python的len()函数:获取列表或字符串的长度Python的len()函数:获取列表或字符串的长度Nov 18, 2023 am 09:23 AM

Python的len()函数:获取列表或字符串的长度,需要具体代码示例一、介绍在Python编程中,len()函数是一个非常常用的内建函数,用于获取列表、元组、字符串等数据类型的长度。这个函数非常简单和方便,可以帮助我们更加高效地处理数据。本文将详细介绍len()函数的用法,并给出一些具体的代码示例。二、len()函数的用法len()函数用于返回给定对象的长

PHP函数介绍—strlen(): 获取字符串的长度PHP函数介绍—strlen(): 获取字符串的长度Jul 24, 2023 am 09:03 AM

PHP函数介绍—strlen():获取字符串的长度在PHP开发中,我们经常需要获取字符串的长度,来进行各种操作。PHP提供了一个非常实用的函数strlen()来获取字符串的长度。本文将向大家介绍这个函数的用法,并给出一些示例代码。strlen()函数的语法如下:intstrlen(string$string)它接受一个参数,即要获取长度的字符串,

用LEN函数计算字符串的长度方法用LEN函数计算字符串的长度方法Jan 28, 2024 am 08:49 AM

如何使用LEN函数统计字符串长度,需要具体代码示例在编程中,经常会遇到需要统计字符串长度的情况,这时可以使用LEN函数来实现。LEN函数是一种常用的字符串函数,它可以返回给定字符串的字符个数,非常方便实用。下面将会介绍如何使用LEN函数来统计字符串长度,并给出具体的代码示例。首先,我们需要了解LEN函数的基本使用方法。LEN函数的语法如下:LEN(strin

Python的len()函数:获取字符串的长度Python的len()函数:获取字符串的长度Nov 18, 2023 pm 12:11 PM

Python的len()函数:获取字符串的长度,需要具体代码示例Python作为一种简单易学且功能强大的编程语言,在字符串操作方面提供了许多便利的函数和方法。其中,len()函数是一个常用的函数,用于获取字符串的长度。在本文中,我们将探讨len()函数的用法,并提供一些具体的代码示例。首先,让我们来看看len()函数的基本用法。len()函数接受一个字符串作

如何使用PHP中的strlen函数获取字符串长度如何使用PHP中的strlen函数获取字符串长度Jun 26, 2023 pm 12:36 PM

strlen()函数是PHP中的一个内置函数,用于获取字符串的字符长度。在许多php项目中,字符串的长度是很重要的数据,这时strlen()函数就变得非常实用了。下面,让我们来介绍如何使用PHP中的strlen函数获取字符串长度。1.基本语法strlen()函数的基本语法是非常简单的,只需要将要获取长度的字符串作为参数传递给函数即可。具体格式如下:<?

在PHP中使用mb_strlen()函数计算字符串长度在PHP中使用mb_strlen()函数计算字符串长度Jun 27, 2023 pm 01:28 PM

在PHP开发中,经常需要对字符串进行计算长度操作。PHP中提供了一个内置函数mb_strlen(),用于计算字符串的长度,特别适用于处理中文字符。在PHP中,字符串的长度可以用strlen()函数来得到。但是,这个函数对于含有非ASCII字符的字符串(包括中文)的统计是有问题的。由于strlen()是根据每个字符占用的字节数来计算的,在某些编码方式下,中文字

字符串长度包括\0吗字符串长度包括\0吗Jun 28, 2023 am 11:46 AM

字符串长度包括\0字符,在C语言中,字符串是由字符数组组成的,以\0作为结尾,这个\0字符用来表示字符串的结束,所以,字符串的长度就是字符数组中字符的个数,包括最后的\0字符。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!