Several program applications of the PHPsubstr () function. The substr () function describes part of the string returned by the substr () function. Syntax: substr (string, start, length ). String: required. Specifies that a part of the string is to be returned. Start: required substr () function introduction
The substr () function returns part of the string.
Syntax: substr (string, start, length ).
- String: required. Specifies that a part of the string is to be returned.
- Start: required. Specifies where the string starts. Positive value: start at the specified position of the string; negative value: start at the specified position from the end of the string; 0: start at the first character of the string.
- Charlist: Optional. Specifies the length of the string to be returned. The default value is until the end of the string. Positive number: return from the position where the start parameter is located; negative number: return from the end of the string.
Note: If start is a negative number and the length is less than or equal to start, the length is 0.
Program List: The start parameter of a negative value.
';$rest = substr("abcdef", -2); // returns "ef"echo $rest.'
';$rest = substr("abcdef", -3, 1); // returns "d"echo $rest.'
';?>
Program running result:
fefd
Program List: length parameter of a negative value
Start from the start position. if the length is negative, it starts from the end of the string. Substr ("abcdef", 2,-1), that is, starting from c, and then-1 indicates intercepting to e, that is, intercepting 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 running 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 be achieved using "square brackets"$string = 'abcdef';echo $string[0]; // aecho '
';echo $string[3]; // decho '
';echo $string[strlen($string)-1]; // fecho '
';?>
Program running result:
bcdefbcdabcdabcdeffadf
Program List: remove suffix
Program running result:
bkjia.jpg
Program List: a string that is too long is displayed only at the beginning and end. it is replaced by a ellipsis in the middle.
30) {$ vartypesf = strrchr ($ file ,". "); // get the total length of the character $ vartypesf_len = strlen ($ vartypesf); // extract the 15 characters on the left $ word_l_w = substr ($ file ); // capture 15 characters on the right $ 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 running result:
Hellothisfileha...andthisfayl.exe
Program List: displays more text as ellipsis
In many cases, we need to display a fixed number of words, and the extra words should be replaced by ellipsis.
$length) return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer; return $string; } ?>
Program running result:
welcome to bkjia, I hope...
Program List: format a string
Sometimes we need to format strings, such as phone numbers.
= $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 running result:
+86 0756 2337788+86 0756 23.37788+86 0756 23.377 a
The http://www.bkjia.com/PHPjc/752408.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752408.htmlTechArticlesubstr () function introduces the substr () function to return part of a string. Syntax: substr (string, start, length ). String: required. Specifies that a part of the string is to be returned. Start: required...