How to remove the first character of string in PHP?
Last Updated :
16 Jul, 2024
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.
Similar Reads
How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in
2 min read
How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t
2 min read
How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_
4 min read
How to Remove Last Character from String in Ruby? Removing the last character from a string in Ruby is a common task in various programming scenarios. Whether you need to manipulate user input, process file paths, or clean up data, there are several approaches to achieve this. This article focuses on discussing how to remove the last character from
2 min read
How to get string between two characters in PHP ? A string is a sequence of characters stored in the incremental form in PHP. A set of characters, one or many can lie between any two chosen indexes of the string. The text between two characters in PHP can be extracted using the following two methods : Table of ContentUsing substr() Method: Using fo
4 min read