How do i remove the first character of a string in php?

Exec time for the 3 answers :

Remove the first letter by replacing the case

$str = "hello";
$str[0] = "";
// $str[0] = false;
// $str[0] = null;
// replaced by �, but ok for echo

Exec time for 1.000.000 tests : 0.39602184295654 sec

Remove the first letter with substr[]

$str = "hello";
$str = substr[$str, 1];

Exec time for 1.000.000 tests : 5.153294801712 sec

Remove the first letter with ltrim[]

$str = "hello";
$str= ltrim [$str,'h'];

Exec time for 1.000.000 tests : 5.2393000125885 sec

Remove the first letter with preg_replace[]

$str = "hello";
$str = preg_replace['/^./', '', $str];

Exec time for 1.000.000 tests : 6.8543920516968 sec

PHPServer Side ProgrammingProgramming


Kickstart HTML, CSS and PHP: Build a Responsive Website

Featured

59 Lectures 8.5 hours

Ogbemudia Terry Osayawe

More Detail

To remove the first character of a string in PHP, the code is as follows−

Example

 Live Demo

Output

This will produce the following output −

Before removing the first character = Test
After removing the first character = est

Example

Let us now see another example

 Live Demo

Output

This will produce the following output−

Before removing the first character = Demo 
After removing the first character = emo

AmitDiwan

Updated on 27-Dec-2019 07:53:32

  • Related Questions & Answers
  • Remove all except the first character of a string in MySQL?
  • How to remove the first and last character in a string in R?
  • How to remove the first character of link [anchor text] in JavaScript?
  • How to find the first character of a string in C#?
  • How to cut only the first character in MySQL string?
  • How to remove a character [‘’] in string array and display result in one string php?
  • How to remove the last character from a string in Java?
  • How to print the first character of each word in a String in Java?
  • Finding the first non-repeating character of a string in JavaScript
  • How to remove first character from column name in R data frame?
  • How to remove a particular character from a String.
  • PHP – How to return the character count of a string using iconv_strlen[]?
  • Java Program to Capitalize the first character of each word in a String
  • Finding the index of the first repeating character in a string in JavaScript
  • Program to find the index of first Recurring Character in the given string in Python

Previous Page Print Page Next Page  

Advertisements

In this tutorial, we will represent to you three main PHP functions used for removing the first character of a string.

Check out the examples and choose the one that suits more for your project.

The first PHP function that you can use for removing the first character of a string is Itrim[].

All you need to do is just to apply the code below:

$str = '::f:o:'; 
$str = ltrim[$str, ':']; 
var_dump[$str]; //=> 'f:o:'

The second way we recommend you to use is the substr[] function. Here is how to run it:

And the last handy function to use for removing the last character of a string is preg_replace[]:

$str = "hello"; 
$str = preg_replace['/^./', '', $str];

The Itrim[] function is supported on PHP 4, PHP 5, and PHP 7 versions. This function is used for returning a string with whitespace stripped from the beginning of the string.

The substr[] is considered a built-in function in PHP, applied for extracting a part of a string.

It is capable of returning the extracted part of a string if successful. On failure, it will return either false or an empty string.

The preg_replace[] function is capable of returning a string or an array of strings. Here, all the matches of a pattern, detected in the output are replaced with substrings.

How do I remove the first character of a string?

To delete the first character from a string, you can use either the REPLACE function or a combination of RIGHT and LEN functions. Here, we simply take 1 character from the first position and replace it with an empty string [""].

How do I remove the first and last character of a string in PHP?

Using trim : trim[$dataList, '*']; This will remove all * characters [even if there are more than one!] from the end and the beginning of the string.

How do you get the first character of a string in PHP?

To get the first character from a string, we can use the substr[] function by passing 0,1 as second and third arguments in PHP.

What is PHP Ltrim?

The ltrim[] function removes whitespace or other predefined characters from the left side of a string. Related functions: rtrim[] - Removes whitespace or other predefined characters from the right side of a string. trim[] - Removes whitespace or other predefined characters from both sides of a string.

Chủ Đề