A very important technique in PHP is to intercept characters of a specified length in a specified string. PHP pre-defined functions for string interception can be used
substr ()function to implement. The following is an introduction to the syntax of the substr () function and its application.
The syntax format for the substr () function is as follows:
SUBSTR (String, start, length);
The substr () function parameters are described below:
Parameters |
Description |
String |
Specifying a String Object |
Start |
Specifies the position at which to begin intercepting the string. If the argument start is negative, The intercept starts at the end of the string |
Length |
An optional parameter that specifies the number of characters to intercept, or if the length is negative, to take the length of the penultimate character |
Description
The specified position of start is calculated from 0, where the position of the first character in the string is represented as 0.
Start is positive data reads characters from left to right
Right-to-left reading of characters when start is negative
Length is the default until the end of the string, positive-starts at the specified position in the string, and negative-starts at the specified position from the end of the string.
Use the substr () function to intercept characters of a specified length in a string, and the code for the instance is as follows:
<?php Echo substr ("ABCDEFG", 0); Capture Echo "<br>" from the No. 0 character; echo substr ("ABCDEFG", 2,4); 4 Characters of echo "<br>" starting from the 2nd character; echo substr ("ABCDEFG", -4,2); The 2-character echo "<br>" starts from the 4th character in the countdown. echo substr ("ABCDEFG", 0,-4); Intercept from the No. 0 character and intercept to the 4th character?>
The output is:
Abcdefgcdefdeabc
In the development of some web programs, in order to maintain a reasonable layout of the entire page, often need for some extra-long text, only required to display some of the information. The following is a concrete example of how it is implemented.
Use the substr () function to intercept partial string information for extra-long text, with the remainder using "..." instead, the sample code is as follows:
<?php $str = "The Shanghai and Shenzhen Stock Exchange has issued a" high transfer "information disclosure guidelines, the implementation of" inquiry "inquiries, the" high transfer "insider trading verification linkage, focused on a group of" high transfer "in the name of engaging in insider trading or information disclosure violation cases. "; if (strlen ($STR) >30) { //if the string length of the text is greater than the echo substr ($str, 0,30). "...."; Output the first 30 bytes of text, and then output the ellipsis }else{ echo $str; If the string length of the text is less than 30, direct output of the original text}?>
The output is:
SHANGHAI-Shenzhen Stock Exchange has been released ....
Description: intercepts a certain length of character from the specified string in the specified position. You can get a portion of a fixed format string by using the substr () function.
Note: using the substr () function when intercepting a string, if the number of truncated strings is odd, it will result in the interception of Chinese characters garbled, so the substr () function for the interception of English strings, if you want to intercept the Chinese string, And to avoid garbled, the best way is to use the substr () function to write a custom function.