Is there a function in PHP that takes in a string, a number (i
), and a character (x
), then replaces the character at position (i
) with (x
)?
If not, can somebody help me in implementing it?
Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.
Definition and Usage The substr_replace() function replaces a part of a string with another string. Note: If the start parameter is a negative number and length is less than or equal to start, length becomes 0. Note: This function is binary-safe.
$str = 'bar'; $str[1] = 'A'; echo $str; // prints bAr
or you could use the library function substr_replace
as:
$str = substr_replace($str,$char,$pos,1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With