string substr ( string source, int start [, int length])
The substr() function allows you to read just part of a string, and takes a minimum of two parameters - the string to work with, and where you want to start reading from. There is an optional third parameter to allow you to specify how many characters you want to read (the length you want to copy). Here are some examples of basic usage:
After that code executes, $a will contain "oodbye, Perl!" If you were expecting it to return the full string, do not worry - it is a common mistake. In PHP, strings an arrays start at 0 rather than 1, and, as we specified 1 as parameter two to substr(), it started copying from the second character and took all the letters to the end of the string.
With $b we get the expected behaviour, the entire string is copied, because we started at index 0. $c has us copying from index 5 (the sixth character), and so $c will be set to "ye, Perl!", as expected. With $d we start from index 50 (the 51st character) in the string "Goodbye, Perl!", which clearly does not exist. This line is in there to show you that even if you try to copy more than you have, PHP will not return an error - it will just return an empty string.
With $e we start using the third parameter. With 5 as parameter two and 4 as parameter 3, substr() should start from index five (the sixth character) and copy four characters. If you try it, you will see that it does just that - substr($string, 5, 4) returns the string "ye, ", a four-letter word with a space making up the last character.
Finally, with variable $f, we have 1 character being copied from index 10, which stores "e" in $f.
So far, so good. However, we're not finished - substr() can do more. For example, you can specify a negative number as parameter three for the length, and PHP will consider that number the amount of characters you wish to omit from the end of the string, as opposed to the number of characters you wish to copy. Consider this script:
$a is quite straightforward, and copies five characters from index five onwards, giving "ye, P". However, $b and $c both use negative lengths, which mean that they take all the string except for length characters at the end. So, $b is set to "ye, Perl", and $c is set to "Goodbye". Using negative lengths allows you to say "copy everything but the last three characters".
But wait - there is even more! You can also use a negative start index, in which case you start copying start characters from the end. You can even use a negative length with your negative start index. Here are some examples that should hopefully make things clear:
That chunk of code demonstrates each possible way to use substr(). You already know how $a, b, and $c work - copy from character five till end, copy five characters from character five, and copy all but the last character respectively.
With $d, we start using a negative start value for parameter 2, -5. After running the code, $d will be set to "Perl!", because PHP will start 5 characters from the end of the string, then copy from there till the end of the string. With $e we specify a negative start and a positive length, with the end result being that PHP will start five characters from the end of the string ("P"), then copy four characters, meaning that $e will be set to "Perl".
Finally, with $f, we have both a negative start value and a negative length, which translates as "start five characters from the end of the string, and copy everything but the last four". As we're starting five from the end and we are asking PHP not to copy the last four, we are in fact copying just one character, "P", which, if you run the script, will be what $f ends up containing. Marvellous.