Home Backend Development PHP Tutorial php字符串截取(UTF-八/GB2312)

php字符串截取(UTF-八/GB2312)

Jun 13, 2016 pm 12:33 PM
return start string substr

php字符串截取(UTF-8/GB2312)

1. 截取GB2312中文字符串

//截取中文字符串
function mysubstr($str, $start, $len) {
??? $tmpstr = "";
??? $strlen = $start + $len;
??? for($i = 0; $i ??????? if(ord(substr($str, $i, 1)) > 0xa0) {
??????????? $tmpstr .= substr($str, $i, 2);
??????????? $i++;
??????? } else
??????????? $tmpstr .= substr($str, $i, 1);
??? }
??? return $tmpstr;
}
?>

2. 截取utf8编码的多字节字符串

//截取utf8字符串
function utf8Substr($str, $from, $len)
{
??? return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
?????????????????????? '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
?????????????????????? '$1',$str);
}
?>
3. UTF-8、GB2312都支持的汉字截取函数

/*?
Utf-8、gb2312都支持的汉字截取函数?
cut_str(字符串, 截取长度, 开始长度, 编码);?
编码默认为 utf-8?
开始长度默认为 0?
*/?
?
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')?
{?
??? if($code == 'UTF-8')?
??? {?
??????? $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";?
??????? preg_match_all($pa, $string, $t_string);?
?
??????? if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
??????? return join('', array_slice($t_string[0], $start, $sublen));?
??? }?
??? else?
??? {?
??????? $start = $start*2;?
??????? $sublen = $sublen*2;?
??????? $strlen = strlen($string);?
??????? $tmpstr = '';?
?
??????? for($i=0; $i??????? {?
??????????? if($i>=$start && $i??????????? {?
??????????????? if(ord(substr($string, $i, 1))>129)?
??????????????? {?
??????????????????? $tmpstr.= substr($string, $i, 2);?
??????????????? }?
??????????????? else?
??????????????? {?
??????????????????? $tmpstr.= substr($string, $i, 1);?
??????????????? }?
??????????? }?
??????????? if(ord(substr($string, $i, 1))>129) $i++;?
??????? }?
??????? if(strlen($tmpstr)??????? return $tmpstr;?
??? }?
}?
?
$str = "abcd需要截取的字符串";?
echo cut_str($str, 8, 0, 'gb2312');?
?>
4. BugFree 的字符截取函数

/**?
?* @package???? BugFree?
?* @version???? $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $?
?*?
?*?
?* Return part of a string(Enhance the function substr())?
?*?
?* @author????????????????? Chunsheng Wang ?
?* @param string? $String? the string to cut.?
?* @param int???? $Length? the length of returned string.?
?* @param booble? $Append? whether append "...": false|true?
?* @return string?????????? the cutted string.?
?*/?
function sysSubStr($String,$Length,$Append = false)?
{?
??? if (strlen($String) ??? {?
??????? return $String;?
??? }?
??? else?
??? {?
??????? $I = 0;?
??????? while ($I ??????? {?
??????????? $StringTMP = substr($String,$I,1);?
??????????? if ( ord($StringTMP) >=224 )?
??????????? {?
??????????????? $StringTMP = substr($String,$I,3);?
??????????????? $I = $I + 3;?
??????????? }?
??????????? elseif( ord($StringTMP) >=192 )?
??????????? {?
??????????????? $StringTMP = substr($String,$I,2);?
??????????????? $I = $I + 2;?
??????????? }?
??????????? else?
??????????? {?
??????????????? $I = $I + 1;?
??????????? }?
??????????? $StringLast[] = $StringTMP;?
??????? }?
??????? $StringLast = implode("",$StringLast);?
??????? if($Append)?
??????? {?
??????????? $StringLast .= "...";?
??????? }?
??????? return $StringLast;?
??? }?
}?
?
$String = "CodeBit.cn -- 简单、精彩、通用";?
$Length = "18";?
$Append = false;?
echo sysSubStr($String,$Length,$Append);?
?>

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
3 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)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

What should I do if docker start cannot start? What should I do if docker start cannot start? Oct 21, 2022 pm 03:43 PM

Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 > /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

See all articles