PHP string replace | How to replace string in PHP

PHP string replace

  • PHP string replace, the str_replace() the function used to replace characters in a text string.
  • PHP str_replace is case-sensitive. If you would like to perform a case-insensitive search, attempt str_ireplace() function.
  • therefore, If the variable specified by the third argument is an array, the function will check each array element and will return an array.
  • If the primary and second function arguments both are arrays, and also the second argument has fewer elements than the first, an empty string is going to be used as a replacement.
  • If the primary argument may be an array and also the second is a string, the string is going to be used for each array component.
  • The PHP string replaces, the function is binary-safe.

Syntax:

str_replace(find, replace, string, count)

there is the simple scheme for correct syntax of the PHP string replace function, in the given below table, every parameter of the function is described separately.

Parameter Description
find Required. Defines the value that has to be found.
replace Required. Defines the replacement value.
string Required. Specifies a string where the search will take place.
count Optional. Sets a variable that counts the changes made.

PHP String Replace Function Explained

By using the str_replace PHP function, therefore with the help of this function, you can replace a string specified in the first argument with the string specified in the second argument.
The third argument is a string or an array in which search and replacement will take place. therefore, PHP str_replace() returns an array or a string with the replaced values.
let’s see the following example:-


PHP String Replace: Using str_replace()

How to replace a word inside a string in PHP?
Answer: To replace a word inside a string using the PHP str_replace() function. let’s see the given following example, to replace all the occurrences of a word within a string. in the given program example the word “other” is replaced by the word “some”. Counting other people’s sins does not make you a saint.

.
Note: The PHP str_replace() the function is case-sensitive, therefore, if you want to perform case-insensitive match and replace then you can use the str_ireplace() function.

2. How to replace the part of a string with another string
Answer: To replace the part of a string with another string, you can use also the PHP str_replace() function, let’s see the below given following example, therefore, in this example substring “Breathe deep” of the string $mystr is replaced by the string “Exercise Everyday” let’s see how it work.


PHP String Replace: Understand More Example

Note: Using str_replace() with an array and a count variable:
Search an array for the value “brown”, and then replace it with “yellow”.

 

Note: Using str_replace() with fewer elements in replace than find:


Understand the Code Example Explanation

In the example, you can see below, finds the word Deer in the $my_arr array, and replaces it with the word Giraffe. therefore, We only have one change done, in this situation but furthermore, the function would successfully replace all the words Deer if the code would consist of more than one.
furthermore, here the variable $i holds a value of how many replacements did the function do:

In the given below example, PHP str_replace the function replaces two strings in the $arr array with the letter B.