搜索
首页php教程php手册用正则,从指定起始位置,在源字符串之中截取定长字符串

[代码]用正则, 从指定起始位置, 在源字符串之中截取定长字符串(含中文)[第四版]
[代码]用正则, 从指定起始位置开始, 在源字符串之中截取一定长度的字符串[第四版]
[代码]使用正则表达式, 从指定的起始位置开始, 在源字符串之中截取一定长度的字符串[第四次修正]
[代码]使用正则表达式, 从字符串头部开始, 在源字符串之中截取一定字节长度的字符串
[代码]使用正则表达式, 从指定的起始位置开始, 在源字符串之中截取一定长度的字符串

(BTW: 中文编码很复杂也有些不合理的地方 高位是 0xa1-0xfe (不含 0xff 因为 0xff即 255在telnet协议中有重要作用), 低位 0x40-0xfe; 而 GBK 为了和 unicode 映射把高位扩展到了 0x81-0xfe


对于最后字节是否截取错误中文的说明:
最后一个字节,假如截取了中文的一半,那么应该是高位字节,其ASCII码大于0x81的。
因为中文的高位字节都是大于0x81的,而低位字节不限。
一个完整的汉字:[0x81-0xfe][0x40-0xfe]
故使用正则表达式,依次取出汉字和非汉字,汉字优先。
最后一个字节,假如截取了中文的一半,那么她将是一个非汉字,而且是汉字的高位字节
而判定这个字节是否在[0x81-0xfe],即可知道是否截取错误。


// ---------------------------------------------------------------
// File name : preg_substr.php
// Description : 使用正则表达式, 从指定的起始位置开始, 在源字符串之中截取一定程度的字符串
// -----------------------------------------------------------

/// 函数说明
/// 函数名称 : preg_substr
/// 函数版本 : 第四次修订
/// 函数功能 : 使用正则表达式, 从指定的起始位置开始, 在源字符串之中截取一定程度的字符串
/// 函数参数 :
/// $strSource : 源字符串
/// $intStart : 起始位置, 默认为0表示从头开始
/// $intLen : 截取长度, 默认为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

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用