UTF-8编码时如何把中文和各种特殊标点符号当作长度为2
这标题有点二。。。不知道怎么描述,将就着看吧。
注册用户时 经常见到的长度限制是:
中文的话可以输入12个,英文的话可以输入24个。
这在PHP要怎么验证呢?
UTF-8编码时
用strlen一个中文的长度是3。
用mb_strlen一个中文的长度是1。
回复内容:
这标题有点二。。。不知道怎么描述,将就着看吧。
注册用户时 经常见到的长度限制是:
中文的话可以输入12个,英文的话可以输入24个。
这在PHP要怎么验证呢?
UTF-8编码时
用strlen一个中文的长度是3。
用mb_strlen一个中文的长度是1。
看来你们都不知道 有个函数名叫 mb_strwidth
UTF-8 编码的字符可能由 1~3个 字节组成,具体数目可以由第一个字节判断出来。(理论上可能更长,但这里假设不超过3个字节)
UTF-8的编码方式
- 第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
- 第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
- 否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)。
获取长度可以指定编码:
mb_strlen($str,'gb2312')
很简单,转换成cp936编码,然后再直接strlen就行了。

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
