PHP Tutorial > String Functions > substr Function
The substr function in PHP is used to retrieve a portion of the string. The syntax of the substr function is:
substr ('string', position_start, substr_length)
where position_start is the starting point (the first character has a position of 0). if position_start is a negative number, the starting position is the -(position_start)th character from the end. substr_length is the length of the string to be returned. If substr_length is a negative number, that means substr_legnth characters will be omitted from the end of the string.
Let's take a look at the examples below:
Example 1
print substr('PHP substr is a substring function', 5, 3);
Result:
ubs
Example 2
print substr('PHP substr is a substring function', -5, 3);
Result:
cti
Example 3
print substr('PHP substr is a substring function', 11, -5);
Result:
is a substring fun
11 means the returned string starts at the 11th position, which is "i" in is. -5 means the last 5 characters in the string are omitted.
Next: PHP trim Function
Link to this page: If you find this page useful, we encourage you to link to this page. Simply copy and paste the code below to your website, blog, or profile.
More Tutorials: SQL CSS HTML
Copyright 2007-2010 1keydata.com All Rights Reserved. Privacy Policy
|