Mb_substr
- (PHP 4 >= 4.0.6, PHP 5, PHP 7)
- Mb_substr-get part of String
- mb_substr-getting a partial string
Description
mb_substr( $str, $start[, $lengthNULL[, $encodingmb_internal_encoding()]] )// Performs a multi-byte safe substr() operation based on number of characters. Position is counted from // the beginning of str. First character's position is 0. Second character position is 1, and so on.//根据字符数执行一个多字节安全的 substr() 操作。 位置是从 str 的开始位置进行计数。 第一个字符的位置是 0。第二个字符的位置是 1,以此类推。
Parametersstr
- The string to extract the substring from.
- Extracts a substring from the string.
Start
- If Start is non-negative, the returned string would start at the start ' th position in str, counting from zero. For instance, with the string ' abcdef ', the character at position 0 are ' a ', the character at position 2 are ' C ', and so forth .
If start is not a negative number, the returned string starts at str start and counts from 0. For example, the string ' abcdef ', where the character of position 0 is ' a ', the character of position 2 is ' C ', and so on.
- If start is negative, the returned string would start at the start ' th character from the end of Str.
If start is a negative number, the returned string starts at the start of the first character at the end of Str.
Length
- Maximum number of characters to use from Str. If omitted or NULL is passed, extract all characters to the end of the string.
- The maximum number of characters to use in Str. If this argument is omitted or NULL is passed in, it is extracted to the end of the string.
Encoding
- The encoding parameter is the character encoding. If It is omitted, the internal character encoding value would be used.
- The encoding parameter is a character encoding. If omitted, the internal character encoding is used.
Return Values
- MB_SUBSTR () returns the portion of STR specified by the start and length parameters.
- The MB_SUBSTR () function returns the portion specified in STR based on the start and length parameters.
Changelog
- 5.4.8-passing NULL as length extracts all characters to the end of the string. Prior to this version NULL is treated the same as 0.
Examples
<?php/*** Created by Phpstorm.* User:zhangrongxiang* Date:2018/1/30* Time: PM 8:51 */$string="0123456789 Hello";/** Start > 0 length > 0*/$mystring=Mb_substr( $string, 5, 1 );Echo $mystring.Php_eol; //5$mystring=Mb_substr( $string, 5, 2 );Echo $mystring.Php_eol; //$mystring=Mb_substr( $string, Ten, 2 );Echo $mystring.Php_eol; //Hello/** Start < 0 length > 0*/$mystring=Mb_substr( $string,-2, 2 );Echo $mystring.Php_eol; //HelloEcho ' Mb_strlen: '.Mb_strlen( $string ).Php_eol;//12$mystring=Mb_substr( $string,-Mb_strlen( $string ), 2 );Echo $mystring.Php_eol; //$mystring=Mb_substr( $string,-3, 2 );Echo $mystring.Php_eol; //9 You/** Start > 0 Length < 0*/$mystring=Mb_substr( $string, 5,-1 );Echo $mystring.Php_eol; //56789 you$mystring=Mb_substr( $string, 0,-Mb_strlen( $string )+1 );Echo $mystring.Php_eol; //0$mystring=Mb_substr( $string, 5,-5 );Echo $mystring.Php_eol; ///** Start < 0 length < 0*/$mystring=Mb_substr( $string,-Ten,-1 );Echo $mystring.Php_eol; //23456789 you$mystring=Mb_substr( $string,-5,-1 );Echo $mystring.Php_eol; //789 youfunctionMb_ucfirst( $str, $enc=' Utf-8 ' ){return Mb_strtoupper( Mb_substr( $str, 0, 1, $enc ), $enc ).Mb_substr( $str, 1, Mb_strlen( $str, $enc ), $enc );}EchoMb_ucfirst( "Hello World hi China" ).Php_eol; //hello World Hello China/** * @param $string * @param string$encoding * * @returnArray */functionGet_character_classes( $string, $encoding="UTF-8" ){$current _encoding=mb_internal_encoding(); mb_internal_encoding( $encoding ); $has=Array(); $stringlength=Mb_strlen( $string, $encoding ); for ( $i=0; $i<$stringlength; $i++){$c=Mb_substr( $string, $i, 1 ); if ( ( $c>="0" )&&( $c<="9" ) ){$has[' numeric ']="Numeric";}Else if ( ( $c>="a" )&&( $c<=$zd ) ){$has[' alpha ']="Alpha"; $has[' Alphalower ']=' Alphalower ';}Else if ( ( $c>="A" )&&( $c<="Z" ) ){$has[' alpha ']="Alpha"; $has[' Alphaupper ']="Alphaupper";}Else if ( ( $c=="$" )||( $c=="£" ) ){$has[' Currency ']="Currency";}Else if ( ( $c=="." )&&( $has[' decimal '] ) ){$has[' decimals ']="Decimals";}Else if ( $c=="." ){$has[' decimal ']="Decimal";}Else if ( $c=="," ){$has[' comma ']="Comma";}Else if ( $c=="-" ){$has[' Dash ']="Dash";}Else if ( $c==" " ){$has[' Space ']="Space";}Else if ( $c=="/" ){$has[' Slash ']="Slash";}Else if ( $c==":" ){$has[' Colon ']="Colon";}Else if ( ( $c>=" " )&&( $c<="~" ) ){$has[' ASCII ']="ASCII";}Else{$has[' binary ']="Binary";} }mb_internal_encoding( $current _encoding ); return $has;}$string="1234asdfa£^_{}|} ~????";foreach (Get_character_classes( $string ) as $k=$v ){Echo $k." : ".$v.Php_eol;}//numeric:numeric//alpha:alpha//alphalower:alphalower//alphaupper:alphaupper//currency:currency//ascii:ascii//binary:binary
Article reference
PHP mb_substr Use