How to remove the first character of string in PHP?
Last Updated :
11 Jul, 2025
Remove the very first character of a given string in PHP Examples:
Input : Geeksforgeeks
Output : eeksforgeeks
Input :, Hello geek!
Output : Hello geek!
Explanation:
In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. removing characters are to be known.
Example:
php
<?php
$str = "geeks";
// Or we can write ltrim($str, $str[0]);
$str = ltrim($str, 'g');
echo $str;
?>
If string is not known and we want to remove characters from beginning then we can use substr()
Here we can use it by two parameters one is the string and the other is the index. substr() return string from the second parameter index to the end of the string.
php
<?php
$str = "geeks";
$str1 = substr($str, 1);
echo $str1."\n";
$str1 = substr($str, 2);
echo $str1;
?>
Using preg_replace()
Using preg_replace(), you can remove the first character of a string with a regular expression. The pattern /^./ matches the first character, and replacing it with an empty string removes it.
Example:
PHP
<?php
$string = "Hello World";
$string = preg_replace('/^./', '', $string);
echo $string; // Output: ello World
?>
Using mb_substr() for multibyte strings
Using mb_substr() in PHP ensures accurate handling of multibyte characters when removing the first character from a string. It takes into account character encoding, ensuring the operation respects the actual character boundaries.
Example:
PHP
<?php
$string = "Hello World";
$string = mb_substr($string, 1); // Remove the first character
echo $string; // Output: "ello World"
?>
Using Array Manipulation
Using array manipulation, you can remove the first character of a string in PHP by converting the string to an array, removing the first element, and then rejoining the array into a string. This method ensures flexible handling of characters.
Example
PHP
<?php
$string = "Hello, World!";
$stringArray = str_split($string);
array_shift($stringArray);
$modifiedString = implode('', $stringArray);
echo $modifiedString . PHP_EOL; // Output: ello, World!
?>
Using ltrim()
The ltrim() function is typically used to remove whitespace or other predefined characters from the beginning of a string. However, we can customize it to remove the first character of a known string.
Example:
PHP
<?php
$string = "Geeksforgeeks";
$result = ltrim($string, $string[0]);
echo $result; // Output: eeksforgeeks
?>
Using str_replace() with str_split()
Using str_replace() in conjunction with str_split(), you can remove the first character of a string by converting the string into an array, replacing the first character, and then joining the array back into a string.
Example
PHP
<?php
function removeFirstCharacter($str) {
// Split the string into an array of characters
$charArray = str_split($str);
// Remove the first character
array_shift($charArray);
// Join the array back into a string
return implode('', $charArray);
}
$input1 = "Geeksforgeeks";
$input2 = ", Hello geek!";
$output1 = removeFirstCharacter($input1);
$output2 = removeFirstCharacter($input2);
echo "Input: $input1\n";
echo "Output: $output1\n";
echo "Input: $input2\n";
echo "Output: $output2\n";
?>
OutputInput: Geeksforgeeks
Output: eeksforgeeks
Input: , Hello geek!
Output: Hello geek!
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.