Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP function to replace a (i)th-position character

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?

like image 549
vikmalhotra Avatar asked Oct 22 '10 06:10

vikmalhotra


People also ask

How can I replace multiple special characters in a string in PHP?

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.

How do I replace Substr?

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.


1 Answers

$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); 
like image 54
codaddict Avatar answered Sep 24 '22 07:09

codaddict