Home php教程 PHP源码 substr加强版,可截取中文字符

substr加强版,可截取中文字符

May 26, 2016 am 08:18 AM

substr加强版,可截取中文字符

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

/*

substr加强版,可截取中文字符,

 

*/

 

function subistr($str,$start,$end){

    $reslut = '';

    for($i=$start ; $i<=$end ; $i++){

        if(preg_match(&#39;/[\x7f-\xff]/&#39;,$str[$i]) ){

            $reslut .= $str[$i].$str[$i+1].$str[$i+2];

            $i = $i+2;

            $end = $end+2;

        }else{

            $reslut .= $str[$i];

        }

    }

    return $reslut?$reslut:false;

}

 

$ch = &#39;0as这ss是带中文的测dd试bnb&#39;;

var_dump(subistr($ch,0,7));

 

//string &#39;0as这ss是带&#39; (length=14)

Copy after login

                   

 以上就是substr加强版,可截取中文字符的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

How to verify whether the input is all Chinese characters in golang How to verify whether the input is all Chinese characters in golang Jun 24, 2023 am 09:16 AM

With the development of the times, we pay more and more attention to the verification of data, especially the verification of user input. For language verification, how to accurately determine whether the input is all Chinese characters has become an important issue. In golang, we can use the unicode package and regexp package to achieve this requirement. 1. Unicode package The unicode package provides a series of core support for Unicode. We can use the functions in this package to accurately determine whether a character is a Chinese character.

How to deal with the pinyin sorting problem of Chinese characters in PHP? How to deal with the pinyin sorting problem of Chinese characters in PHP? Sep 05, 2023 pm 05:00 PM

How to deal with the pinyin sorting problem of Chinese characters in PHP? When developing Chinese websites or applications, we often face the need to sort Chinese strings according to pinyin. However, due to the complexity of Chinese characters, directly using conventional sorting algorithms will lead to errors in sorting results. Therefore, we need to use a special method to deal with the pinyin sorting problem of Chinese characters. In PHP, a common solution is to use a pinyin library such as "Overtrue/Pinyin". This is a pinyin based on PHP

How to sort Chinese characters in C language environment? How to sort Chinese characters in C language environment? Feb 18, 2024 pm 02:10 PM

How to implement Chinese character sorting function in C language programming software? In modern society, the Chinese character sorting function is one of the essential functions in many software. Whether in word processing software, search engines or database systems, Chinese characters need to be sorted to better display and process Chinese text data. In C language programming, how to implement the Chinese character sorting function? One method is briefly introduced below. First of all, in order to implement the Chinese character sorting function in C language, we need to use the string comparison function. Ran

How to use regular expressions to match Chinese characters in PHP How to use regular expressions to match Chinese characters in PHP Jun 22, 2023 am 09:16 AM

In PHP, regular expression is a commonly used string matching tool. It can be used to determine whether a string conforms to a specific format, thereby verifying the validity of the input value. When processing Chinese characters, since Chinese characters and English characters are encoded differently, the matching rules of the regular expression need to be adjusted accordingly. This article will introduce how to use regular expressions to match Chinese characters in PHP. 1. Understand Chinese character encoding. Commonly used character encodings in PHP are UTF-8 and G.

How to get the first letter of Chinese characters in PHP? How to get the first letter of Chinese characters in PHP? Sep 06, 2023 am 11:18 AM

How to get the first letter of Chinese characters in PHP? When processing Chinese characters, sometimes we need to get the first letter of the Chinese character. PHP provides some built-in functions and extension packages to achieve this functionality. A common way is to use the mb_substr() function in combination with the ord() function. The mb_substr() function is used to obtain the substring of a string, and the ord() function is used to obtain the ASCII code value of a character. We can get the first letter of the Chinese character by getting the first character and its ASCII code value.

Detailed explanation of string interception method in Go language Detailed explanation of string interception method in Go language Mar 13, 2024 am 08:03 AM

Detailed explanation of string interception methods in Go language. In Go language, strings are immutable byte sequences, so some methods need to be used to implement string interception. String interception is a common operation to obtain a specific part of a string. You can intercept the first few characters, the last few characters of the string, or a certain length of characters from a specific position according to your needs. This article will introduce in detail how to intercept strings in Go language and provide specific code examples. Using slicing to implement string interception In Go language, you can use slicing to

PHP regular expression practice: matching Chinese characters PHP regular expression practice: matching Chinese characters Jun 22, 2023 pm 08:34 PM

In the process of developing projects using PHP, we often encounter the need to process Chinese characters. Regular expressions are a powerful text processing tool that can help us match and process Chinese characters quickly and accurately. In this article, I will introduce related techniques and examples on how to use PHP regular expressions to match Chinese characters. Matching Chinese Characters First, we need to understand how Chinese characters are represented in the computer. Normally, Chinese characters are represented using Unicode encoding. In Unicod

How to use PHP to write a search box that automatically converts Chinese characters into pinyin initials? How to use PHP to write a search box that automatically converts Chinese characters into pinyin initials? Sep 05, 2023 pm 06:21 PM

How to use PHP to write a search box that automatically converts Chinese characters into pinyin initials? With the widespread use of Chinese on the Internet, in order to facilitate users to retrieve information, we often need to provide a search function based on the first letter of Pinyin. In this article, we will introduce how to write a search box in PHP that can convert Chinese characters into the first letters of Pinyin. First, we need to prepare a library for converting Chinese to Pinyin. In PHP, we can use the open source library pinyin.php to implement this function. you can pass

See all articles