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

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기
