PHP substr functions of the implementation of functional skills to explain _php tutorial

Source: Internet
Author: User
Tags tidy
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:

 
  
  
  1. < ? PHP
  2. $ Str "www.designline.cn";
  3. Echo substr ($STR, 4);
  4. ?>

Output: designline.cn

2. The PHP substr function intercepts 3 characters from the right, similar to the one in asp:

 
  
  
  1. <? PHP
  2. $ Str "www.designline.cn";
  3. Echo substr ($str,-3);
  4. ?>

Output:. cn

3. The PHP substr function intercepts 6 characters starting from the 4th character:

 
  
  
  1. < ? PHP
  2. $ Str "www.designline.cn";
  3. Echo substr ($str, 4,6);
  4. ?>

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):

 
  
  
  1. < ? PHP
  2. $ Str "< |>www.designline.cn< |>";
  3. Echo substr ($str, 3,-3);
  4. ?>

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.