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.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read