Replace one time

$myVar="abcd ABCD abcd";

$myVar1=str_ireplace("b", "",$myVar);

[b]result1[/b]

acd ACD acd

$myVar2=str_replace("b", "",$myVar);

[b]result2[/b]

acd ABCD acd

The code above prouduces the result above.

I like to replace “b” just one time.
The would-be code below doesn’t work correctly, but I hope it show what I want.

[b]would-be code[/b]

$myVar3=str_ireplace("b", "",$myVar, [COLOR="#FF0000"]1[/COLOR]);

[b]target result[/b]

acd ABCD abcd

How can I get my target result above?

You can use strpos then do a substr_replace

The following trials seems not to work.

[b]trial1[/b]

substr_replace(2, $myVar)

[b]trial2[/b]

substr_replace("b", $myVar,2)


<?php
$string = 'abcdef abcdef abcdef';


if(false !== ($index = strpos($string, 'b'))){
	$string[$index] = null;
}


echo $string;