Explanation on the implementation skills of PHP substr function_PHP tutorial

WBOY
Release: 2016-07-15 13:30:57
Original
1056 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

Output: www.designline.cn


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446260.htmlTechArticleI just sorted out the usage of the substr function and made a few examples to solve the confusion of newcomers. Experts please Float by. Let’s first take a look at the syntax of the PHP substr function: string substr(string...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!