Replace one time

$myVar="abcd ABCD abcd";

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


acd ACD acd

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


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.


substr_replace(2, $myVar)


substr_replace("b", $myVar,2)

$string = 'abcdef abcdef abcdef';

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

echo $string;