The younger brother not to tidy up the use of substr function, do a few examples to solve the confusion of the new, master please drift over.
Let's take a look at the syntax of the PHP substr function:
String substr (string string, int start, int [length])
Argument string is the string to manipulate
The argument start is the starting position of the string you want to intercept, and if start is negative, the length character is truncated from the countdown start.
The optional parameter is length of the string you want to intercept, and if you do not specify it, the end of the string is taken by default. If length is negative, it is the position from start to the right to the end of the last number of length characters.
At first, it may feel awkward to use this function, but if you understand the syntax of PHP substr function, then his function than the ASP in the left and right, Brahmins, very useful. Let's take a look at his usage in the following example:
1, from the beginning of the 4th character intercept to the end of the string, similar to the left in asp:
- < ? PHP
- $ Str "www.designline.cn";
- Echo substr ($STR, 4);
- ?>
Output: designline.cn
2. The PHP substr function intercepts 3 characters from the right, similar to the one in asp:
- <? PHP
- $ Str "www.designline.cn";
- Echo substr ($str,-3);
- ?>
Output:. cn
3. The PHP substr function intercepts 6 characters starting from the 4th character:
- < ? PHP
- $ Str "www.designline.cn";
- Echo substr ($str, 4,6);
- ?>
Output: Design
4, sometimes we know the beginning and end of a string, the middle is an indefinite length of characters, at this time in addition to PHP substr function of the regular outside we can also be implemented with substr (of course, to get the middle character method has n, this is only an example of SUBSTR application):
- < ? PHP
- $ Str "< |>www.designline.cn< |>";
- Echo substr ($str, 3,-3);
- ?>
Output: www.designline.cn
http://www.bkjia.com/PHPjc/446260.html www.bkjia.com true http://www.bkjia.com/PHPjc/446260.html techarticle The younger brother not to tidy up the use of substr function, do a few examples to solve the confusion of the new, master please drift over. Let's take a look at the syntax of the PHP substr function: String substr (String ...