How to remove the first character of string in PHP?


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

Example

 Live Demo

<?php
   $str = "Test";
   echo "Before removing the first character = ".$str;
   $res = substr($str, 1);
   echo "
After removing the first character = ".$res; ?>

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

<?php
   $str = "Demo";
   echo "Before removing the first character = ".$str;
   $res = ltrim($str, 'D');
   echo "
After removing the first character = ".$res; ?>

Output

This will produce the following output−

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

Updated on: 27-Dec-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements