SUBSTR () function Introduction
The substr () function returns a portion of a string.
Syntax: substr (string,start,length).
- String: Required. A string that specifies the part to return.
- Start: Required. Specifies where to start the string. Positive number-starts at the specified position in the string, minus-starts at the specified position from the end of the string, and 0-begins at the first character in the string.
- Charlist: Optional. Specifies the length of the string to return. The default is until the end of the string. Positive number-returns from the position where the start parameter is located; Negative number-returned from the end of the string.
Note: If start is negative and length is less than or equal to start, length is 0.
Program List: The start parameter for negative values
'; $rest = substr ("ABCdef",-2); Returns "EF" Echo $rest. '
'; $rest = substr ("ABCdef",-3, 1); Returns "D" Echo $rest. '
';? >
Program Run Result:
Fefd
Program List: The length parameter of negative value
It starts at the start position, and if the length is negative, it starts at the end of the string. substr ("ABCdef", 2,-1), that is, start with C, then-1 to intercept E, is to intercept the CDE.
'; $rest = substr ("ABCdef", 2,-1); Returns "CDE" Echo $rest. '
'; $rest = substr ("ABCdef", 4,-4); Returns "" Echo $rest.
'; $rest = substr ("ABCdef",-3,-1); Returns "De" echo $rest. '
';? >
Program Run Result:
Abcdecdede
Program List: Basic substr () function usage
'; Echo substr (' abcdef ', 1, 3); Bcdecho '
'; Echo substr (' abcdef ', 0, 4); Abcdecho '
'; Echo substr (' abcdef ', 0, 8); Abcdefecho '
'; Echo substr (' abcdef ',-1, 1); Fecho '
';//accessing single characters in a string//can also is achieved using "square brackets" $string = ' abcdef '; Echo $string [0]; Aecho '
'; echo $string [3]; Decho '
'; Echo $string [Strlen ($string)-1]; Fecho '
';? >
Program Run Result:
Bcdefbcdabcdabcdeffadf
Program List: Remove suffix
Program Run Result:
Bkjia.jpg
Program List: Strings that are too long are only displayed in the middle, with ellipses instead of
{$VARTYPESF = STRRCHR ($file, "."); Get the total length of the character $vartypesf_len = strlen ($VARTYPESF); Intercept left 15 characters $word_l_w = substr ($file, 0,15); Intercept right 15 characters $word_r_w = substr ($file,-15); $word _r_a = substr ($word _r_w,0,-$vartypesf _len); return $word _l_w. " ... ". $word _r_a. $vartypesf; } else return $file; }//Return:hellothisfileha...andthisfayl.exe $result = Funclongwords ($file); Echo $result;? >
Program Run Result:
Hellothisfileha...andthisfayl.exe
Program List: Displays the extra text as an ellipsis
Many times we need to display a fixed number of words, the number of words with an ellipsis instead.
$length) return (Preg_match ('/^ (. *) \w.*$/', substr ($string, 0, $length + 1), $matches)? $matches [1]: substr ($string , 0, $length)). $replacer; return $string; }?>
Program Run Result:
Welcome to Bkjia, I hope ...
Program List: Formatting strings
Sometimes we need to format a string, like a phone number.
= $FormatPos) {//if its a number = stores it If (Is_numeric (substr ($Format, $FormatPos, 1))) { $Result. = substr ($String, $StringPos, 1); $StringPos + +; If It is not a number = stores the caracter} else {$Result. = substr ($Format, $FormatPos, 1); }//next Caracter at the mask. $FormatPos + +; } return $Result;} For phone numbers at Buenos Aires, argentina//Example 1: $String = "8607562337788"; $Format = "+00 0000 0000000"; Echo Str_format_number ($String, $Format); Echo '
';//Example 2: $String = "8607562337788"; $Format = "+00 0000 00.0000000"; Echo Str_format_number ($String, $Format); Echo '
';//Example 3: $String = "8607562337788"; $Format = "+00 0000 00.000 a"; Echo Str_format_number ($String, $Format); Echo '
';? >
Program Run Result:
+86 0756 2337788+86 0756 23.37788+86 0756 23.377 A
http://www.bkjia.com/PHPjc/752408.html www.bkjia.com true http://www.bkjia.com/PHPjc/752408.html techarticle the substr () function describes the substr () function that returns part of a string. Syntax: substr (string,start,length). String: Required. A string that specifies the part to return. Start: Must ...