search
HomeBackend DevelopmentPHP Tutorial 截取GBK字符串的有关问题~

截取GBK字符串的问题~~~~
这是一个截取GBK字符串的函数:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
function  gb_substr( $str ,  $len ){        
      $count  = 0;        
      for ( $i =0;  $i 


我的问题是,GBK和GB2312每个字符不肯定是2个字节吗?那直接用要截取的长度*2不完了吗?
比如我要截取3个字符:3*2=6 也就是我要截取到6的位置。

这么想对吗?

------解决方案--------------------
使用mb系列函数,GBK对非ASCII两个字符,ASCII还是1个字节。
------解决方案--------------------
不对,因为有可能含ascii码的数字或字符

探讨
这是一个截取GBK字符串的函数:
PHP code

function gb_substr( $str , $len ){
$count = 0;
for ( $i =0; $i if ( $count == $len ) break……
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
一个ascii字符占几个字节一个ascii字符占几个字节Mar 09, 2023 pm 03:49 PM

一个ascii字符占1个字节。ASCII码字符在计算机中采用7位或8位二进制编码表示,并保存在一个字节中,即一个ASCII码占用一个字节。ASCII码可分为标准ASCII码和扩展ASCII码,其中标准ASCII码也叫基础ASCII码,使用7位二进制数(剩下的1位二进制为0)来表示所有的大写和小写字母,数字0到9、标点符号,以及在美式英语中使用的特殊控制字符。

Go语言编码解析:UTF-8与GBK对比Go语言编码解析:UTF-8与GBK对比Mar 28, 2024 pm 01:54 PM

Go语言编码解析:UTF-8与GBK对比在Go语言中,处理字符串编码是常见的任务之一。其中,UTF-8和GBK是两种常用的字符编码方式。本文将对UTF-8和GBK进行详细对比,探讨它们的区别和使用方法,并附上具体的代码示例。一、UTF-8与GBK的简介UTF-8:UTF-8是一种可变长度的Unicode编码方式,它可以表示世界上几乎所有语言的字符。UTF-8

counta和count的区别counta和count的区别Nov 20, 2023 am 10:01 AM

Count函数用于计算指定范围内数字的个数。它忽略文本、逻辑值和空值,但会将空单元格计算在内,Count函数只计算包含实际数字的单元格数量。而CountA函数用于计算指定范围内非空单元格的个数。它不仅计算包含实际数字的单元格,还计算包含文本、逻辑值和公式等非空单元格的数量。

快速了解 PHP 中的 ASCII 数值转换快速了解 PHP 中的 ASCII 数值转换Mar 28, 2024 pm 06:42 PM

PHP中的ASCII数值转换是编程中经常会遇到的问题。ASCII(AmericanStandardCodeforInformationInterchange)是一种用于将字符转换为数字的标准编码系统。在PHP中,我们经常需要通过ASCII码来实现字符和数字之间的转换。本文将介绍如何在PHP中进行ASCII数值转换,并给出具体的代码示例。一、将字符

PHP返回字符串第一个字符的 ASCII 值PHP返回字符串第一个字符的 ASCII 值Mar 21, 2024 am 11:01 AM

这篇文章将为大家详细讲解有关PHP返回字符串第一个字符的ASCII值,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP返回字符串第一个字符的ASCII值引言在php中,获取字符串第一个字符的ASCII值是一个常见的操作,涉及到字符串处理和字符编码基础知识。ASCII值用于表示字符在计算机系统中的数字值,对于字符比较、数据传输和存储至关重要。过程获取字符串第一个字符的ASCII值涉及以下步骤:获取字符串:确定要获取ASCII值的字符串。它可以是变量、字符串常量

PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串Mar 21, 2024 am 10:31 AM

这篇文章将为大家详细讲解有关PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP中使用substr()函数从字符串中提取子字符串substr()函数可从字符串中提取指定范围内的字符。其语法如下:substr(string,start,length)其中:string:要从中提取子字符串的原始字符串。start:子字符串开始位置的索引(从0开始)。length(可选):子字符串的长度。如果未指定,则提

unicode和ascii有哪些区别unicode和ascii有哪些区别Sep 06, 2023 am 11:56 AM

unicode和ascii的区别包括编码范围不同、存储空间不同和兼容性不同等。详细介绍:1、编码范围不同,ascii的编码范围是0-127,主要用于表示英语字母,而unicode的编码范围广阔得多,可以表示几乎所有的语言字符;2、存储空间不同,ascii通常使用1个字节来存储一个字符,而unicode可能使用2个或更多的字节来存储一个字符;3、兼容性不同等等。

MySQL关于Count函数的用法区别总结MySQL关于Count函数的用法区别总结Nov 29, 2022 pm 04:47 PM

本文给大家介绍MySQL关于Count函数的用法区别,统计数据的需求在我们日常开发中是非常容易遇到了,MySQL也支持多种的计算的函数,接下来我们来看一看他们之间有什么区别,以及他们是否存在一些坑。

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
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment