PHP substr: Main Tips
substris one of the most common PHP string functions. It returns a part of a string.
- The first argument specifies a string which will be trimmed.
- The second argument specifies the position of the trim start. The third argument specifies the length of the string to be returned.
- The start and length arguments can also be negative numbers.
substr function returns the trimmed string. In other words, the method extracts a part of a string. In case of failure or an empty string,
False is returned.
echo substr("Random text", 6);
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid certificates of completion
- Professional service
- Flexible timetables
- A variety of features to choose from
- Professional certificates of completion
- University-level courses
- Multiple Online degree programs
- Great user experience
- Offers quality content
- Very transparent with their pricing
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
Syntax: Scheme and Parameters
As with all PHP string functions, using correct syntax is crucial here. Let's look at how the correct syntax for PHP
substr function should look like:
substr(string, start, length)
Now, let's go through every element step by step to get a better understanding of what each parameter of the
substr PHP function represents:
|string||Necessary. Specifies the exact string to trim.|
|start||Necessary. Sets a position for the start of trimming (counting from the start of the string if the number is positive, from the end - if it's negative).
0 sets start at the first character.
|length||Optional. Sets the length of the returned string (measured from the start parameter is a the number is positive, from the end - if it's negative).
The default value is 0.
Note: If the second argument has a negative value and the third argument is less than or equal to the second argument, the third argument (length) is automatically set to 0.
More Code Examples
The example below displays a few cases of using the start argument alone in the
substr PHP function. The comments next to every case explain what will be returned:
echo substr("Lama is the best pet", 8); echo substr("Lama is the best pet", 1); echo substr("Lama is the best pet", 5); echo substr("Lama is the best pet", -3); echo substr("Lama is the best pet", -8);
Now, the example below displays cases when start and length arguments are used together. Again, the comments next to every case display what will be returned:
echo substr("Lama is the best pet", 0, 16); echo substr("Lama is the best pet", 2, 9); echo substr("Lama is the best pet", 0, 4); echo substr("Lama is the best pet", 0, -4); echo substr("Lama is the best pet", -9, -4);
Latest Udacity Coupon Found:
75% OFF COURSES
Udacity Black Friday Offer
The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!