Substr --- obtain some strings
Syntax: string substr (string, int start [, int length])
Note:
Substr () returns a string that is specified by the start and length parameters.
If start is a positive number, the returned string starts with the start character of the string.
Example:
Copy codeThe Code is as follows:
<? Php
$ Rest = substr ("abcdef", 1); // returns "bcdef"
$ Rest = substr ("abcdef", 1, 3); // returns "bcd"
?>
If start is a negative number, the returned string starts with the start character at the end of the string.
Example:
Copy codeThe Code is as follows:
<? Php
$ Rest = substr ("abcdef",-1); // returns "f"
$ Rest = substr ("abcdef",-2); // returns "ef"
$ Rest = substr ("abcdef",-3, 1); // returns "d"
?>
If the length parameter is given and it is a positive number, the returned string will return length characters from start.
If the length parameter is given and it is a negative number, the returned string ends with the nth length character ending with the string.
Example:
Copy codeThe Code is as follows:
<? Php
$ Rest = substr ("abcdef", 1,-1); // returns "bcde"
?>
Dongyue, 11:10:41
Substr --- obtain some strings
Syntax: string substr (string, int start [, int length])
Note:
Substr () returns a string that is specified by the start and length parameters.
If start is a positive number, the returned string starts with the start character of the string.
Example:
Copy codeThe Code is as follows:
<? Php
$ Rest = substr ("abcdef", 1); // returns "bcdef"
$ Rest = substr ("abcdef", 1, 3); // returns "bcd"
?>
If start is a negative number, the returned string starts with the start character at the end of the string.
Example:
Copy codeThe Code is as follows:
<? Php
$ Rest = substr ("abcdef",-1); // returns "f"
$ Rest = substr ("abcdef",-2); // returns "ef"
$ Rest = substr ("abcdef",-3, 1); // returns "d"
?>
If the length parameter is given and it is a positive number, the returned string will return length characters from start.
If the length parameter is given and it is a negative number, the returned string ends with the nth length character ending with the string.
Example:
Copy codeThe Code is as follows:
<? Php
$ Rest = substr ("abcdef", 1,-1); // returns "bcde"
?>
Chinese character truncation functions supported by Utf-8 and gb2312
Copy codeThe Code is as follows:
// Truncate a Chinese String
/*
Chinese character truncation functions supported by Utf-8 and gb2312
Cut_str (string, truncation length, start length, encoding );
The default encoding format is UTF-8.
The default start length is 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 <$ 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 = "a good website for my feet ";
Echo cut_str ($ str, 8, 5, 'gb2312 ');