Parameter Explanation and Usage Explanation of PHP substr of Function

  • 2021-08-16 23:24:50
  • OfStack

substr(string,start,length)

Parameters:

1. string is the string you want to intercept

2. start is the starting position to be intercepted (0 means starting from the first character of the number from front to back, and negative number means counting from back to front)

eg: start=1, which means to intercept from the second number starting from front to back, start=-1, which means to intercept from the first number starting from back to front.

3. When length is a positive number, it is the length to be intercepted; When it is negative, it is understood as removing the last few characters

eg: length=3, which means truncating 3 lengths; length=-2, that is, the last two characters are removed

Example (from the manual, the explanation is guaranteed to be understood by the age of 3):

1. echo substr ("Hello world",-10,-2);

Output: ello wor

Explanation:-10: What does it mean? You should be clear! Haha, of course, it means starting from the 10th character from the back to the front. Let's count 1, count carefully, d l r o w _ (don't forget the space) o l l e, stop well, just start cutting from your e, don't use the front H

-2: What does it mean? You said I know, indicating that it is best not to use two characters! Awesome! It's like that! So ld has nothing to do with you!  

Are you clear after reading it?

2. If you can't understand that example 1 (?) Then let's look at another simple introduction example

echo substr("Hello world",6,6);

Output: world

Explanation: 6: It means starting from the 7th character (7 7 7 oh), of course it is w;

6: Of course, it means intercepting 6 lengths (what, you said world only has 5 lengths, which is really witty, and you have found it all. Of course, intercepting it at the end of the end will return without intercepting it)

The following describes the use of substr function of php

Always can't remember, write a pen:


<?php 
echo substr("Hello world!",6);  Output world! , When there are only two parameters, the 1 The string when the parameter table is, the 2 Represents a parameter from the 6 Start with, intercept all the rest, and start from 0 At first, spaces count 1 Bit 
?>

<?php
echo substr("Hello world!",6,4);  Output worl   Have 3 Parameters, the 2 Parameters are the starting position, the 3 Parameters are 1 Length of common truncation 
?>

Summarize


Related articles: