1.substr (source string, in fact position [, length])-truncate string to return part of string
- <? PHP
- $str ="phpddt.com";
- echo substr($str,2); pddt.com
- echo substr($str,2,3); PDD
- echo substr($str,-2); Om negative number starts at the end of the fetch
- ?>
But when you intercept the Chinese string, it is easy to garbled, because a Chinese character is two bytes, and an English letter is a byte. The solution is as follows:
2.mb_substr (), using the same method and substr, but to open the php.ini inside the extension=php_mbstring.dll extension, do not worry, the general space quotient
will open this extension.
- <? PHP
- echo mb_substr("php dot-pass",1,3,"UTF-8"); HP Point
- ?>
There are many Chinese string interception tutorials on the internet, which is more complex to implement, and it is better to use PHP's own functions to implement them. The network data (PHP code) is organized as follows:
(1) Intercept GB2312 Chinese string
- <? PHP
- Intercept GB2312 Chinese string
- function mysubstr($str, $start, $len){
- $tmpstr ="";
- $strlen = $start + $len;
- For($i =0; $i < $strlen; $i+ +){
- If(ord(substr($str, $i,1))>0xa0){
- $tmpstr . = substr($str, $i,2);
- $i+ +;
- }Else
- $tmpstr . = substr($str, $i,1);
- }
- return $tmpstr;
- }
- echo mysubstr("php dot-pass",1,5); PHP point
- ?>
(2) Intercept UTF8 encoded multibyte string
- <? PHP
- Intercept UTF8 string
- 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 ',
- ' $ ',$str);
- }
- echo utf8substr("php dot-pass",1,5); HP Point -to-point
- ?>
(3) Supports Chinese character interception function supported by Utf-8 and gb2312
- <? PHP
- At the same time support Utf-8, gb2312 both support the Chinese character interception function, the default encoding is Utf-8
- 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); (count ( $t _string[< Span class= "lit" >0) - $start > $sublen ) return join< Span class= "pun" > ( ", 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<$strlen; $i+ +)
- {
- If($i>=$start && $i< ($start+$sublen))
- {
- 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) <$strlen ) $tmpstr. ="...";
- return $tmpstr;
- }
- }
- $str ="PHP Point-and-click provides original PHP tutorial";
- echo cut_str($str,8,0); PHP dot-Pass provides ...
- ?>
PHP Intercept string