php 不同编码下的字符串长度区分_PHP
复制代码 代码如下:
//编码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 '
';
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java string length refers to the number of characters in a character object. In java, each character has a Unicode value, and a java string is a sequence of Unicode characters. Therefore, the length of a Java string is calculated by the number of Unicode characters in the string object. Java string is one of the most commonly used data types in the Java language and can be used to store text data.

PHP function introduction—strlen(): Get the length of a string In PHP development, we often need to get the length of a string to perform various operations. PHP provides a very practical function strlen() to get the length of a string. This article will introduce you to the usage of this function and give some sample code. The syntax of the strlen() function is as follows: intstrlen(string$string) It accepts one parameter, which is the string to get the length,

Python's len() function: Get the length of a list or string, you need specific code examples 1. Introduction In Python programming, the len() function is a very commonly used built-in function, used to get lists, tuples, and strings. The length of the data type. This function is very simple and convenient and can help us process data more efficiently. This article will introduce the usage of len() function in detail and give some specific code examples. 2. Usage of len() function The len() function is used to return the length of a given object.

How to use the LEN function to count the length of a string requires specific code examples. In programming, you often encounter situations where you need to count the length of a string. In this case, you can use the LEN function to achieve this. The LEN function is a commonly used string function. It can return the number of characters in a given string and is very convenient and practical. The following will introduce how to use the LEN function to count the length of a string and give specific code examples. First, we need to understand the basic usage of the LEN function. The syntax of the LEN function is as follows: LEN(strin

Python's len() function: Get the length of a string, specific code examples are required. As an easy-to-learn and powerful programming language, Python provides many convenient functions and methods for string operations. Among them, the len() function is a commonly used function used to obtain the length of a string. In this article, we will explore the usage of the len() function and provide some concrete code examples. First, let's look at the basic usage of the len() function. The len() function accepts a string as

The strlen() function is a built-in function in PHP used to obtain the character length of a string. In many PHP projects, the length of the string is very important data, and the strlen() function becomes very useful at this time. Next, let us introduce how to use the strlen function in PHP to get the string length. 1. Basic syntax The basic syntax of the strlen() function is very simple. You only need to pass the string whose length you want to obtain as a parameter to the function. The specific format is as follows: <?

The length of the string includes the \0 character. In C language, the string is composed of a character array and ends with \0. This \0 character is used to indicate the end of the string. Therefore, the length of the string is the character array. The number of characters, including the last \0 character.

In PHP development, it is often necessary to calculate the length of strings. PHP provides a built-in function mb_strlen(), which is used to calculate the length of a string, especially suitable for processing Chinese characters. In PHP, the length of a string can be obtained using the strlen() function. However, this function has problems with statistics on strings containing non-ASCII characters (including Chinese). Since strlen() is calculated based on the number of bytes occupied by each character, in some encoding methods, Chinese characters
