How to Remove the First Character of a String with PHP
In this tutorial, we will represent to you three main PHP functions used for removing the first character of a string.
Check out the examples and choose the one that suits more for your project.
The first PHP function that you can use for removing the first character of a string is Itrim().
All you need to do is just to apply the code below:
$str = '::f:o:'; $str = ltrim($str, ':'); var_dump($str); //=> 'f:o:'
The second way we recommend you to use is the substr() function. Here is how to run it:
$str = substr($str, 1);
And the last handy function to use for removing the last character of a string is preg_replace():
$str = "hello"; $str = preg_replace('/^./', '', $str);
Describing the Itrim Function
The Itrim() function is supported on PHP 4, PHP 5, and PHP 7 versions. This function is used for returning a string with whitespace stripped from the beginning of the string.
Describing the substr Function
The substr() is considered a built-in function in PHP, applied for extracting a part of a string.
It is capable of returning the extracted part of a string if successful. On failure, it will return either false or an empty string.
Describing the preg_replace Function
The preg_replace() function is capable of returning a string or an array of strings. Here, all the matches of a pattern, detected in the output are replaced with substrings.