How to String Replace First Character in PHP?

03-Apr-2023

.

Admin

How to String Replace First Character in PHP?

Hi Dev,

In this tutorial we will go over the demonstration of how to string replace first character in php?. Here you will learn how to remove the first character of string in php?. let’s discuss about how to replace only the first occurrence of a string in php?. I would like to share with you php replace first occurrence in string with code examples.

There are example to Check if string replace first character in PHP. in this example, we will use to ltrim() and substr() both function to Check if string replace first character. so Let's the following example with output.

Example 1:


index.php

<?php

$str = "Nicesnippets.com";

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

$strnew = ltrim($str, 'N');

echo $strnew;

?>

Output:

icesnippets.com

Example 2:

index.php

<?php

$str = "Nicesnippets";

$str1 = substr($str, 1);

echo $str1."\n";

$str1 = substr($str, 2);

echo $str1;

?>

Output:

icesnippets,

cesnippets

I hope it could help you...

#PHP