How to replace string in PHP ?

Last Updated On Wednesday 25th Aug 2021

php string replace

When it comes to replacing multiple values at a time, take two associative arrays.
One is for find words, another replacement.

	1. str_replace(find-word, replace-word, $str, $variable[optional]); // case sensitive
2. str_ireplace(find-word, replace-word, $str, $variable[optional]); // case insensitive
# Replace the existing string.
# $variables are to find how many values have been replaced.

The Above can use this function with an array to replace any value.
$arr1 = str_replace('find', 'replacement', $arr);

	$str = 'hello world!, the world is beautiful';
$arr_find = ['hello', 'world'];
$arr_replace = ['hi', 'earth'];
str_replace($arr_find, $arr_replace,$str);
return $str = 'hi earth, this earth is beautiful';
  • Replace the string from start to length.
	substr_replace($str, replacement, start, length[optional])

  • replace multiple words with an associative array. Where key is found, and replacement is value.
	$strtr($str, 'find','replacement');
#replace multiple character