用正則,從指定起始位置,在源字符串之中截取定長(zhǎng)字符串
Jun 13, 2016 am 10:02 AM[代碼]用正則, 從指定起始位置, 在源字符串之中截取定長(zhǎng)字符串(含中文)[第四版]
[代碼]用正則, 從指定起始位置開(kāi)始, 在源字符串之中截取一定長(zhǎng)度的字符串[第四版]
[代碼]使用正則表達(dá)式, 從指定的起始位置開(kāi)始, 在源字符串之中截取一定長(zhǎng)度的字符串[第四次修正]
[代碼]使用正則表達(dá)式, 從字符串頭部開(kāi)始, 在源字符串之中截取一定字節(jié)長(zhǎng)度的字符串
[代碼]使用正則表達(dá)式, 從指定的起始位置開(kāi)始, 在源字符串之中截取一定長(zhǎng)度的字符串
(BTW: 中文編碼很復(fù)雜也有些不合理的地方 高位是 0xa1-0xfe (不含 0xff 因?yàn)?0xff即 255在telnet協(xié)議中有重要作用), 低位 0x40-0xfe; 而 GBK 為了和 unicode 映射把高位擴(kuò)展到了 0x81-0xfe
對(duì)于最后字節(jié)是否截取錯(cuò)誤中文的說(shuō)明:
最后一個(gè)字節(jié),假如截取了中文的一半,那么應(yīng)該是高位字節(jié),其ASCII碼大于0x81的。
因?yàn)橹形牡母呶蛔止?jié)都是大于0x81的,而低位字節(jié)不限。
一個(gè)完整的漢字:[0x81-0xfe][0x40-0xfe]
故使用正則表達(dá)式,依次取出漢字和非漢字,漢字優(yōu)先。
最后一個(gè)字節(jié),假如截取了中文的一半,那么她將是一個(gè)非漢字,而且是漢字的高位字節(jié)
而判定這個(gè)字節(jié)是否在[0x81-0xfe],即可知道是否截取錯(cuò)誤。
// ---------------------------------------------------------------
// File name : preg_substr.php
// Description : 使用正則表達(dá)式, 從指定的起始位置開(kāi)始, 在源字符串之中截取一定程度的字符串
// -----------------------------------------------------------
/// 函數(shù)說(shuō)明
/// 函數(shù)名稱(chēng) : preg_substr
/// 函數(shù)版本 : 第四次修訂
/// 函數(shù)功能 : 使用正則表達(dá)式, 從指定的起始位置開(kāi)始, 在源字符串之中截取一定程度的字符串
/// 函數(shù)參數(shù) :
/// $strSource : 源字符串
/// $intStart : 起始位置, 默認(rèn)為0表示從頭開(kāi)始
/// $intLen : 截取長(zhǎng)度, 默認(rèn)為32
function preg_substr($strSource, $intStart=0, $intLen=32)
{
is_int($intLen) ?0:die("len isn't a integer");
is_int($intStart) ?0:die("start isn't a integer");
if ($intStart>=0 && $intLen>0 && @preg_match('/^(.{'.$intStart.'})(.{0,'.$intLen.'})/si', $strSource)) {
@preg_match('/^(.{'.$intStart.'})(.{0,'.$intLen.'})/si', $strSource, $regs);
@preg_match_all('/([x81-xFE].|.)/sim', $regs[1], $regs1, PREG_PATTERN_ORDER);
@preg_match('/^[x81-xFE]$/',$regs1[1][count($regs1[1])-1])?$intStart--:0;
@preg_match('/^(.{'.$intStart.'})(.{0,'.$intLen.'})/si', $strSource, $regs);
@preg_match_all('/([x81-xFE].|.)/sim', $regs[2], $regs1, PREG_PATTERN_ORDER);
@preg_match('/^[x81-xFE]$/',$regs1[1][count($regs1[1])-1])?$intLen--:0;
@preg_match('/^(.{'.$intStart.'})(.{0,'.$intLen.'})/si', $strSource, $regs);
$strResult = $regs[2];
}else{
$strResult = "";
}
return $strResult;
}
function preg_substr2($strSource, $intStart=0, $intLen=32)
{
is_int($intLen) ?0:die("len isn't a integer");
is_int($intStart) ?0:die("start isn't a integer");
if ($intStart>=0 && $intLen>=0)
{
$strResult = substr($strSource, 0, $intStart);
@preg_match_all('/([x81-xFE].|.)/sim', $strResult, $regs, PREG_PATTERN_ORDER);
if(@preg_match('/^[x81-xFE]$/',$regs[1][count($regs[1])-1], $regs)){
$intStart--;
}
$strResult = substr($strSource, $intStart, $intLen);
@preg_match_all('/([x81-xFE].|.)/sim', $strResult, $regs, PREG_PATTERN_ORDER);
if(@preg_match('/^[x81-xFE]$/',$regs[1][count($regs[1])-1], $regs)){
$strResult = substr($strSource, $intStart, --$intLen);
}
}
return $strResult;
}
$strHTML =
ab

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for "PowerPlatformTool" and click the Install button

There are a total of 20 origami birds in Croaker Film and Television Park on Star Dome Railway. Many players don’t know where the origami birds are in Crocker Film and Television Park. The editor has summarized the locations of each origami bird to help everyone. Search for it, and take a look at this latest summary of the locations of the origami birds in Croaker Film and Television Park for specific content. Guide to the Honkai Star Dome Railway: Origami Bird in Crook Movie Park Location 1, Crook Movie Park 1st Floor 2, and Crook Movie Park 2nd Floor Star Dome Railway

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

1. We open Meituan on the mobile phone, and then click on the takeout option in the upper left corner of the homepage. 2. After entering the takeout platform page, you can see the section with daily coupons on the homepage, click on it directly. 3. After entering the Tiantian God Voucher, you will see a lot of activities, click Finish, and then we can get rewards after completing the tasks.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

As a well-known short video platform in China, Kuaishou provides many creators with opportunities to showcase their talents and share their lives. When uploading a video, some novice creators may be confused about how to change the video posting location. This article will introduce you to how to change the publishing location of Kuaishou videos, and share some tips for Kuaishou video publishing to help you make better use of this platform to showcase your work. 1. Where is Kuaishou published and how to change its location? 1. Publishing interface: In Kuaishou APP, click the "Publish" button to enter the video publishing interface. 2. Location information: In the publishing interface, there is a "Location" column. Click to enter the location selection interface. 3. Change location: In the location selection interface, click the "Location" button to view the current location. If you want to change the location, click "Location"

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not

Strings in GoLang, although immutable, can be dynamically modified using the following technique: concatenating strings using string concatenation. Create a new string using string formatting. Modify the underlying byte slice of the string. Use mutable string types provided by third-party libraries.
