Syntax:
Substr (string to be intercepted, start position, and truncation length)
The start position starts from 0. If you want to extract from the first character, the start position parameter is 0.
The last parameter is optional. If only the start position is provided, it is truncated from the start position to the end.
Let's take a look at the example from left to right:
1. Capture from 2nd characters to the endCopy codeThe Code is as follows: $ result = substr ("abcdef", 1 );
Echo ($ result );
The output result is bcdef.
2. extract 3 from 2nd charactersCopy codeThe Code is as follows: $ result = substr ("abcdef", 1, 3 );
Echo ($ result );
The output result is bcd.
Truncate from right to left:
1. extract 1 character from right to leftCopy codeThe Code is as follows: $ result = substr ("abcdef",-1 );
Echo ($ result );
Output result: f
2. Extract 2 Characters from right to leftCopy codeThe Code is as follows: $ result = substr ("abcdef",-2 );
Echo ($ result );
Output result: ef
3. extract 1 character from the right side of 3rd characters to the leftCopy codeThe Code is as follows: $ result = substr ("abcdef",-3, 1 );
Echo ($ result );
Output result: d