PHP: substr()
Substr() is used to return only a certain part of a string, specified by an upper and lower boundary.
PHP SubStr() Prototype
String (return value): This is the piece of the string that lies between the given boundaries, and will be returned by substr().
String (parameter): This is the string that contains the subset of characters we want to return using substr().
Start: This indicates how many positions from the beginning we want to start returning from. Alternatively, if this is a negative number, i.e -x, it will start returning x positions from the end.
Length: This is optional. This represents how many characters from the start position we wish to return. If this is given as a negative, substr() will stop returning characters from that many positions from the end.
How to Use SubStr() in PHP
Let's say we have a string:
Assume we want to return the name, without the delimiting slashes. This means we want to start 2 positions from the left (after the second "/") and stop 1 position before the end (before the last "/"). We would do this using substr() as follows:
$name should now contain "John Doe".
Now, what if we don't know the length of the characters we want to return, but we do know we want to leave off the last "/"? In that case, use substr() in this way:
This will accomplish the same thing.
Comment:
:|
not $name = substr($name, 2, -1); this will return "John Doe"
am i right?
or this is what we want?
oic..i think i know..hehe:)