Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to Remove Last Character from String in Ruby?
Next article icon

How to remove the first character of string in PHP?

Last Updated : 16 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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; ?> 

Output
eeks 

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;  ?> 

Output
eeks eks 

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 ?> 

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" ?> 

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! ?> 

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  ?> 

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"; ?> 

Output
Input: 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.



Next Article
How to Remove Last Character from String in Ruby?

K

Komal Agarwal11
Improve
Article Tags :
  • PHP
  • Technical Scripter
  • Web Technologies
  • PHP-string

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, i
    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 PHP Table of Content Using str_split() method - The st
    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 First Character of a String in SQL?
    SQL (Structured Query Language) is essential for managing and querying relational databases. Whether you're handling customer data, employee records, or product details, SQL provides powerful tools for manipulating string data. One common task when working with strings is to extract the first charac
    4 min read
  • How to Remove the Last Character From a Table in SQL?
    SQL (Structured Query Language) allows for efficient data manipulation and retrieval. A common task in SQL involves removing the last character from a specific column within a table. This can be achieved using string functions like SUBSTRING() and LEN(). In this article, we will demonstrate how to a
    5 min read
  • How to remove First and Last character from String in Scala?
    In this article, we will explore different approaches to removing the first and last character from a string in Scala. Table of Content Using substring methodUsing drop and dropRight methodsUsing substring methodIn this approach, we are using the substring method which takes two arguments the starti
    1 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 Content Using substr() Method: Using f
    4 min read
  • Remove Last character from String in Linux
    In this article, we will discuss how to remove the last character from the string in Linux. In Linux, there are various commands and techniques present by which you can do this task in an easier way if you have some basic knowledge about the commands in Linux. Here, you will see the different comman
    3 min read
  • How to replace multiple characters in a string in PHP ?
    A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP. Using the str_repl
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences