PHP String Replace First Number Example

Nov 23, 2022



PHP String Replace First Number Example

Hi Dev,

In this example, you will learn php string replace first number example. I would like to show you replace first number in php - string. you can understand a concept of php replace first number with code examples. I’m going to show you about how to use function replace first number ?.

In this example string replace first number in PHP. in this example, we will use to preg_replace() function to replace first Number. so Let's see following example with output.

Example : String Replace First Number



// create string

$str ="Hell2";

// Replace First Number

$newStr = preg_replace('/2/', 'o', $str, 1);

echo $newStr;




I hope it could help you...