In this tutorial, we are going to learn how to remove the first n characters of a string in PHP.
Consider, we have the following string:
Now, we want to remove the first 2 characters
pa From the string above.
Note: In PHP, strings are a sequence of characters that can be accessed using its character index, where the first character index is 0, the second character is 1, and the first character index is -1, and so on.
To remove the first n characters of the string, we can use meaning
substr() Function in PHP.
substr() The function gets three arguments, the first is
string, The second is the starting position, the third is the length (optional).
Here is an example, delivering the first 2 characters
pa M e
$place="paris"; $result = substr($place, 2); echo $result;
In the example above, we moved on
2 As a second argument to
substr() function. So it starts the extraction in the index
2, And extracts the rest of the string.
substr() The function does not change the original string, instead it creates a new string with the changed data.