6/19/2023 0 Comments Php substring![]() With 5 as parameter two and 4 as parameter 3, substr() should start from index five (the sixth character) and copy four characters. With $e we start using the third parameter. 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 $d we start from index 50 (the 51st character) in the string "Goodbye, Perl!", which clearly does not exist. $c has us copying from index 5 (the sixth character), and so $c will be set to "ye, Perl!", as expected. ![]() With $b we get the expected behaviour, the entire string is copied, because we started at index 0. 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. Here are some examples of basic usage:Īfter 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. There is an optional third parameter to allow you to specify how many characters you want to read (the length you want to copy). 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. String substr ( string source, int start )
0 Comments
Leave a Reply. |