Date:

Share:

How to remove the first n characters of a string in PHP

Related Articles

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.

Remove the first n characters

To remove the first n characters of the string, we can use meaning substr() Function in PHP.

God 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 wire:

$place="paris";
$result = substr($place, 2);
echo $result;

Productivity:

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.

Note: e substr() The function does not change the original string, instead it creates a new string with the changed data.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles