Use regular, from the specified starting position, in the source string to intercept the fixed-length string _php tutorial

Source: Internet
Author: User
[Code] uses regular, from the specified starting position, in the source string to intercept the fixed length string (including Chinese) [fourth edition]
[Code] uses regular, starting from the specified starting position, in the source string to intercept a certain length of string [fourth edition]
[code] uses regular expressions, starting at the specified starting position, to intercept a certain length of string in the source string [fourth correction]
[code] uses regular expressions, starting at the head of a string, to intercept a string of bytes of length in the source string
[code] uses regular expressions, starting at the specified starting position, to intercept a certain length of string in the source string

(BTW: The Chinese code is very complex also some unreasonable place high is 0xa1-0xfe (does not contain 0xff because 0xFF namely 255 in the Telnet protocol has the important function), the low 0x40-0xfe, but GBK in order to and the Unicode mapping to extend the high position to the 0X81 -0xfe


Description of whether the last byte intercepts errors in Chinese:
The last byte, if it intercepts half of the Chinese, then it should be a high-bit byte, whose ASCII code is greater than 0X81.
Because the high byte of Chinese is greater than 0x81, and the low byte is not limited.
A complete kanji: [0x81-0xfe][0x40-0xfe]
Therefore, the use of regular expressions, and then take out Chinese and non-Chinese characters, Chinese characters first.
The last byte, if it intercepts half of the Chinese, then she will be a non-Chinese character, and it is the high byte of the Chinese character
To determine if the byte is in [0x81-0xfe], you can know whether to intercept the error.


// ---------------------------------------------------------------
File name:preg_substr.php
Description: Use regular expressions to intercept a certain amount of string from the source string, starting at the specified starting position
// -----------------------------------------------------------

Function description
Function Name: PREG_SUBSTR
Function Version: Fourth revision
function function: Use regular expressions to intercept a certain amount of string from the source string, starting at the specified starting position
Function parameters:
$strSource: Source string
$intStart: Starting position, default = 0 means starting from scratch
$intLen: Intercept length, default is 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], $regs 1, Preg_pattern_order);
@preg_match ('/^[x81-xfe]$/', $regs 1[1][count ($regs 1[1])-1])? $intStart--:0;

@preg_match ('/^ (. {'). $intStart. '}) (. {0, '. $intLen. '}) /si ', $strSource, $regs);
@preg_match_all ('/([x81-xfe].|.) /sim ', $regs [2], $regs 1, Preg_pattern_order);
@preg_match ('/^[x81-xfe]$/', $regs 1[1][count ($regs 1[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

http://www.bkjia.com/PHPjc/631416.html www.bkjia.com true http://www.bkjia.com/PHPjc/631416.html techarticle [code] uses regular, from the specified starting position, in the source string to intercept the fixed length string (including Chinese) [fourth] [code] with the regular, starting from the specified starting position, intercepted in the source string ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.