I just sorted out the usage of the substr function and made a few examples to clear up the confusion for newcomers. Experts please pass by.
Let’s first take a look at the syntax of the PHP substr function:
string substr(string string, int start, int [length])
The parameter string is the character to be operated on String
The parameter start is the starting position of the string you want to intercept. If start is a negative number, it means intercepting length characters starting from the penultimate start.
The optional parameter length is the character you want to intercept. String length. If not specified when using, it will be taken to the end of the string by default. If length is a negative number, it means that it is intercepted from start to the right to the penultimate length character at the end
You may feel awkward when using this function at first, but if you understand the syntax of the PHP substr function , then its function is better than left and right in asp, and it is very easy to use. Let's take an example to see its usage:
1. Intercept from the 4th character to the end of the string, similar to left in asp:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">"www.designline.cn"</span><span>; </span></li><li class="alt"><span>echo substr($str,4); </span></li><li><span class="tag">?></span><span> </span></span></li></ol>
Output: designline.cn
2. The PHP substr function intercepts 3 characters from the right, similar to right in asp:
<ol class="dp-xml"><li class="alt"><span><span class="tag">< ?</span><span class="tag-name">php</span><span> </span></span></li><li><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">"www.designline.cn"</span><span>; </span></li><li class="alt"><span>echo substr($str,-3); </span></li><li><span class="tag">?></span><span> </span></span></li></ol>
Output:.cn
3. The PHP substr function starts from the 4th Start intercepting 6 characters:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">"www.designline.cn"</span><span>; </span></li><li class="alt"><span>echo substr($str,4,6); </span></li><li><span class="tag">?></span><span> </span></span></li></ol>
Output: design
4. Sometimes we know the beginning and end of a string, and there are characters of variable length in the middle. In this case, in addition to using In addition to the regularization of the PHP substr function, we can also use substr to implement it (of course there are N ways to get the middle character, this is just an example of the application of substr):
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">"< |>www.designline.cn< |>"</span><span>; </span></span></li> <li class="alt"><span>echo substr($str,3,-3); </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
Output: www.designline.cn