How to remove the first character of string in PHP?

03-Apr-2023

.

Admin

Hi Guys,

In this example, I will learn you how to remove the first character of string in php.you can easy and simply remove the first character of string in php.

Example 1:


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

$str = "NiceSnippests";

$str1 = substr($str, 1);

echo $str1."\n";

$str1 = substr($str, 2);

echo $str1;

?>

Output:

iceSnippests

ceSnippests

Example 2:

In PHP to remove characters from 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.

<?php

$str = "NiceSnippests";

// Or we can write ltrim($str, $str[0]);

$str = ltrim($str, 'g');

echo $str;

?>

Output:

iceSnippests

It will help you...

#PHP