Pulling out a piece of a string can be done with careful application of regular expressions, but if the piece is always at a known character position, this is inefficient. Instead, you should use
. This function takes three arguments: a string value, a start position (measured like it was measured for
), and a length, like so:
$s = substr(
The start position works like
: the first character is zero, the second character is one, and so on. The length is the number of characters to grab at that point: a length of zero means no characters, one means get the first character, two means two characters, and so on. (It stops at the end of the string, so if you ask for too many, it's no problem.) It looks like this:
$hello = "hello, world!";
$grab = substr($hello, 3, 2); # $grab gets "lo"
$grab = substr($hello, 7, 100); # 7 to end, or "world!"
You could even create a "
ten to the power of " operator for small integer powers, as in:
$big = substr("10000000000",0,$power+1); # 10 ** $power
If the count of characters is zero, an empty string is returned. If either the starting position or ending position is less than zero, the position is counted that many characters from the end of the string. So
for a start position and
(or more) for the length gives you the last character. Similarly,
for a start position starts with the second-to-last character like this:
$stuff = substr("a very long string",-3,3); # last three chars
$stuff = substr("a very long string",-3,1); # the letter "i"
If the starting position is before the beginning of the string (like a huge negative number bigger than the length of the string), the beginning of the string is the start position (as if you had used
for a starting position). If the start position is a huge positive number, the empty string is always returned. In other words, it probably does what you expect it to do, as long as you expect it to always return something other than an error.
Omitting the length argument is the same as if you had included a huge number for that argument - grabbing everything from the selected position to the end of the string.[
If the first argument to
is a scalar variable (in other words, it could appear on the left side of an assignment operator), then the
itself can appear on the left side of an
assignment operator. This may look strange if you come from a C background, but if you've ever played with some dialects of BASIC, it's quite normal.
What gets changed as the result of such an assignment is the part of the string that would have been returned had the
been used on the right-hand side of the expression instead. In other words,
returns the fourth and fifth characters (starting at
, for a count of
), so assigning to that changes those two characters for
$hw = "hello world!";
substr($hw, 0, 5) = "howdy"; # $hw is now "howdy world!"
The length of the replacement text (what gets assigned into the
) doesn't have to be the same as the text it is replacing, as it was in this example. The string will automatically grow or shrink as necessary to accommodate the text. Here's an example where the string gets shorter:
substr($hw, 0, 5) = "hi"; # $hw is now "hi world!"
and here's one that makes it longer:
substr($hw, -6, 5) = "nationwide news"; # replaces "world"
The shrinking and growing are fairly efficient, so don't worry about using them arbitrarily, although it is faster to replace a string with a string of equal length.