Home Backend Development PHP Tutorial PHP utf-8 Chinese interception without garbled characters (string judgment version)

PHP utf-8 Chinese interception without garbled characters (string judgment version)

Aug 08, 2016 am 09:30 AM
count offset substr

/*E4 B8 AD E5 9B BD   中国
1110 0100    10111000   10101101
1110 0101    1001101110111101

61  a
0011 1101


0xxx xxxx ,1个字节
110xx xxxx , 2个字节
1110 xxxx, 3
1111 0xxxx 4...
*/<pre name="code" class="php">$str = '中华人aaa民共b和国,c万岁';

function utf8sub($str,$len){
	if($len <=0) return &#39;&#39;;
	$length = strlen($str);
	$offset=0;
	$chars=0;
	$res=&#39;&#39;;
	while($chars<$len && $offset<$length){
		$heigh = decbin(ord(substr($str, $offset,1)));
		echo $heigh ,&#39;</br>';
		if(strlen($heigh)<8){
			$count=1;
		}else if(substr($heigh,0,3) == '110'){
			$count=2;
		}else if(substr($heigh,0,4) == '1110'){
			$count=3;
		}else if(substr($heigh,0,5) == '11110'){
			$count=4;
		}else if(substr($heigh,0,6) == '111110'){
			$count=5;
		}else if(substr($heigh,0,7) == '1111110'){
			$count=6;
		}
		$res .= substr($str, $offset,$count);
		$chars += 1;
		$offset += $count;
	}
	return $res;
}

echo utf8sub($str,10);
Copy after login

Copy after login
Copy after login

The above introduces PHP utf-8 Chinese interception without garbled characters (string judgment version), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between counta and count The difference between counta and count Nov 20, 2023 am 10:01 AM

The Count function is used to count the number of numbers in a specified range. It ignores text, logical values, and null values, but counts empty cells. The Count function only counts the number of cells that contain actual numbers. The CountA function is used to count the number of non-empty cells in a specified range. It not only counts cells containing actual numbers, but also counts the number of non-empty cells containing text, logical values, and formulas.

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

Summary of usage differences of MySQL Count function Summary of usage differences of MySQL Count function Nov 29, 2022 pm 04:47 PM

This article introduces to you the usage differences of MySQL’s Count function. The need for statistical data is very easy to encounter in our daily development. MySQL also supports a variety of calculation functions. Next, let’s take a look at the differences between them. , and whether they have some pitfalls.

Understand the substr() function in PHP to intercept strings Understand the substr() function in PHP to intercept strings Nov 18, 2023 am 11:27 AM

Understand the substr() function in PHP for intercepting strings. In the PHP language, the substr() function is a very useful string processing function, which can be used to intercept string fragments at a specified position and length. The substr() function accepts three parameters: the string to be intercepted, the starting position of the interception, and the length of the interception. Below we will introduce the use of the substr() function in detail and give specific code examples. Basic usage of substr() function substr() function

PHP Notice: Undefined offset solution PHP Notice: Undefined offset solution Jun 25, 2023 am 09:51 AM

PHPNotice:Undefinedoffset is a common PHP program error. It means that the program attempts to use a subscript that does not exist in the array, causing the program to fail to run normally. This error usually occurs when the PHP interpreter displays the following warning message: Notice: Undefinedoffset. Here are some ways to resolve the PHPNotice:Undefinedoffset error: Check the code First, it should

What is the usage of count function in php What is the usage of count function in php Sep 09, 2021 pm 05:08 PM

The usage of the count function in PHP is: [count(array,mode);], where the parameter array specifies the array to be counted, and the parameter mode specifies the mode of the function.

How does java Count count elements in a stream? How does java Count count elements in a stream? May 11, 2023 pm 04:07 PM

Note 1. Count is a terminal operation that can count the total number of elements in the stream. The return value is of long type. 2. count() returns the count of elements in the stream. This is a special case of induction (an induction operation takes a sequence of input elements and combines them into a summary result by repeatedly applying the combining operation). This is a terminal operation and may have consequences and side effects. After a terminal operation is performed, the pipe is considered consumed and cannot be reused. Example // Verify whether there is a string in the list starting with a, and match the first one, that is, return truebooleananyStartsWithA=stringCollection.stream().anyMatch((s

See all articles