You can extract a range of characters using the substr() function.
Specify the starting index and the number of characters you wish to return.
Start the slice at index 6 and continue for 5 positions.
$x |
Note: The first character is at index 0. |
By omitting the length parameter, the range will extend to the end of the string.
Begin the slice at index 6 and continue to the end.
$x |
Use negative indexes to initiate the slice from the end of the string.
Retrieve the 3 characters starting from the “o” in “world” (index -5).
$x |
Keep in mind that the last character has an index of -1. |
Use a negative length to indicate how many characters to skip, beginning from the end of the string.
From the string “Hi, how are you?”, extract the characters starting from index 5 and continue until the 3rd character from the end (index -3).
The result should be “ow are y”.
$x |