• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / PHP / Remove First Character from String Using php

Remove First Character from String Using php

March 13, 2022 Leave a Comment

To remove the first character from a string in php, the easiest way is with php substr() function.

$string = "This is a string variable";

$string_without_first_char = substr($string,1);

echo $string_without_first_char;

// Output:
his is a string variable

You can also use the php ltrim() function.

$string = "This is a string variable";

$string_without_first_char = ltrim($string,$string[0]);

echo $string_without_first_char;

// Output:
his is a string variable

When using string variables in php, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove characters from a string variable.

For example, we can easily remove the first character from a string in php. To remove the first character from a string in php, we can use the substring function substr().

To remove the first character in a string using substr(), we know that the first character has index ‘0’. So, to get everything except the first character, we start our substring at the ‘1’ position.

Below is an example of how to get rid of the first character in a string using php.

$string = "This is a string variable";

$string_without_first_char = substr($string,1);

echo $string_without_first_char;

// Output:
his is a string variable

You can also use substr() to remove the last character from a string in php in a very similar way. To remove the last character in a string, pass ‘0’ as the start position and ‘-1’ as the end position.

$string = "This is a string variable";

$string_without_last_char = substr($string,0,-1);

echo $string_without_last_char;

// Output:
This is a string variabl

Using the php ltrim() Function to Remove Last Character from String

In php, the string method ltrim() removes the characters from the left side of the string that is given to it.

We can use the rtrim() method to remove the first character of a string by passing the first character of the string to the function.

Below is how you can use the ltrim() function in php to get rid of the first character in a string.

$string = "This is a string variable";

$string_without_first_char = ltrim($string,$string[0]);

echo $string_without_first_char;

// Output:
his is a string variable

How to Remove the First N Characters from String Using php

We can also use the substr() function to get rid of the first n characters of a string easily in php.

To remove the first n characters from a string using php, you just need to change the input to the end position argument of the slice you want.

For example, to remove the first 5 characters from a string, you pass ‘5’ as shown in the following php code.

$string = "This is a string variable";

$string_without_first_5_char = substr($string,5);

echo string_without_first_5_char;

// Output:
is a string variable

How to Remove the Last and Last Character from a String Using php

Another example of how we can use substr() to remove characters from a string in php is the removal of the first and first character from a string.

With substr()again, we can easily get rid of the first and last character from a string.

To do so, we need to select all of the elements between the first and last character.

Below is how we can remove the first and last character from a string in php.

$string = "This is a string variable";

$string_without_first_or_first_char = substr($string,1,-1);

echo $string_without_first_or_first_char;

// Output:
his is a string variabl

How to Remove a Character at a Specific Position in a String Using php

The first example I want to share with you in this article is how to remove a character from a string at a specific position.

Let’s say we want to remove the sixth character from a string.

To do so, we can create two substrings from our original string and then concatenate them. We first create a string from the start of our string to the desired position, and then create another string from the desired position + 1 to the end.

Below is an example of how to use substr() in php to remove a character at a specific position.

$string = "This is a string variable";

$string_without_sixth_char = substr($string,0,6) . substr($string,6+1);

echo $string_without_sixth_char;

// Output:
This i a string variable

Hopefully this article has been beneficial for you to learn how to remove the first character from a string in your php code.

Other Articles You'll Also Like:

  • 1.  How to Check If String Contains Substring in PHP
  • 2.  php json_encode() Function – Get JSON Representation of Variables in php
  • 3.  php array_merge() – Merge One or More Arrays Together by Appending
  • 4.  php preg_match_all – Get All Matches of Pattern in String
  • 5.  php floor – Round Down and Find the Floor of a Number in php
  • 6.  php max – Find Maximum Value of Array in php
  • 7.  php array_pop() – Remove Last Element from Array
  • 8.  Check if Variable Exists in php with isset() Function
  • 9.  Capitalize First Letter of String with php ucfirst() Function
  • 10.  Using strtolower() in php to Convert String to Lowercase

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy