PHP STRING REPLACE

php string replacement

In this tutorial, we will learn about PHP string replacement which means how we replace the occurrence of the search string with the replacing string using the str_replace() function in PHP.

The str_replace() function replaces some characters with some other characters in a string. The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement PHP string or array of replacement strings in the given string or array respectively. 

Syntax :

str_replace(find,replace,string,count)

This function follows some rules while working, which are given below :

πŸ‘ͺ If the string which is to be searched is an array, it returns an array.

πŸ‘ͺ If the string to be searched is an array then search and replace is performed with each element of the array.

πŸ‘ͺ If both (search) and (replace) are arrays and (replace) has fewer elements than the (search) array, then an empty string will be used as a replacement.

πŸ‘ͺIf the (search) is an array, but (replace) is a string, then the (replace) string will be used for every search value.

Parameters :

πŸ‘ͺ search (mandatory) – This parameter is a mandatory parameter that can have both string and array-type values. The (search) parameter contains the value which is going to be searched for PHP string replacement.

πŸ‘ͺ replace (mandatory) – This parameter is a mandatory parameter that is replaced with the search values. In simple words – this parameter holds that value which will replace PHP string as (search) value in the (string).

πŸ‘ͺ string (mandatory) – This parameter is also a mandatory parameter which is an array or string in which search and replace value is searched and replaced. It is the string or array with which we are working.

πŸ‘ͺ count (mandatory) – It is the last and optional parameter. It is an integer variable that counts the number of replacements done in the string. Simply, this variable stores the total number of replacements performed on a string (string).

Example :

Basic implementation of string replace PHP :

<?php  
$string = "North America";  
$search = 'North';  
$replace = 'South';  
echo '<b>'."String before replacement:".'</br></b>';  
echo $string.'</br>';  
$newstr = str_replace($search, $replace, $string, $count);  
echo '<b>'."New replaced string is:".'</br></b>';  
echo $newstr.'</br>';  
echo 'Number of replacement ='.$count;  
?>

Output :

String before replacement:
North America
New replaced String is:
South America
Number of Replacement=1

The above example shows that “North” is replaced with “South” and the number of replacements is only 1.

Example :

To replace the multiple values in the string, we have to take an array to store these values for replacement i.e. also possible in PHP to replace the string.

<?php  
$string = "The American Civil War was a civil war in the United Kingdom. It was fight between the Union and the Confederacy, the latter formed by states that had seceded";  
$search = array("Kingdom", "fight");  
$replace = array("States", "fought");  
echo '<b>'."String before replacement:".'</br></b>';  
echo $string.'</br>';  
$newstr = str_replace($search, $replace, $string, $count);  
echo '<b>'."New replaced string is:".'</br></b>';  
echo $newstr.'</br>';  
echo 'Number of replacement ='.$count;  
?>

Output :

String before replacement:
The American Civil War was a civil war in the United Kingdom. It was fght between the Union and the Confederacy, the latter formed by states that had seceded
New replaced String:
The American Civil War was a civil war in the United States. It was fought between the Union and the Confederacy, the latter formed by states that had seceded
Number of replacement=2

PHP String Replace Now we arrive at the string replace tutorial of PHP. if you can understand these few functions, it should be easy for you to manipulate the strings. The string replaces function is extremely useful in managing strings.

This string function looks for all occurrences of β€œgood” and replaces it with β€œnice” in our string variable $str. PHP runs through and does exact matches for each and every instance, which means if we had the word β€œgood” in that string a billion times, PHP would replace it with the word β€œnice” a billion times.

Note: You can also use string replace as delete characters or words from a string by making the second parameter β€œ. Replace the word with an empty string.

Example :

<?php
$str = "Developer helps is good website for developers.";
echo str_replace("good","nice",$str);
?>

Output :

Developer helps is nice website for developers.

String Replace Multiple Occurrence

In the below example, Given Multiple occurrences of the “good” word.

<?php
$str = "Developer helps is good website for good developers.";
echo str_replace("good","nice",$str);
?>

Output :

Developer helps is nice website for nice developers.

Forth parameter, you can put a number that tells PHP the number of characters or words you want to replace.
If you were to search for “Good”, you would not have changed anything.

Note: This function is case-sensitive. Use the str_ireplace ( ) function to perform a case-insensitive search.

Example :

<?php
$str = "Developer helps is good website for developers.";
echo str_ireplace("Good","nice",$str);
?>

Output :

Developer helps is nice website for developers.

If the string to be searched is an array, it returns an array.

Example:

<?php
$arr = array("JAVA","PHP","LARAVEL","WORDPRESS");
print_r(str_replace("JAVA","JSP",$arr,$i));
echo "<br>" . "Replacements: $i";
?>

Output :

Array
(
    [0] => JSP
    [1] => PHP
    [2] => LARAVEL
    [3] => WORDPRESS
)
Replacements: 1

Replace Array in PHP

If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as a replacement.

Example :

<?php
$find = array("Hello","developers");
$replace = array("Dear");
$arr = array("Hello","developers","!");
print_r(str_replace($find,$replace,$arr));
?>

Output :

Array ( [0] => Dear [1] => [2] => ! )

What are the regular expressions in PHP?

Regular expressions are pattern-matching algorithms that can be performed in a single expression.
For creating a complex expression, Regular expressions use arithmetic operators such as (+,-,^).

  • Regular expressions can identify the patterns in the string by calling a single function.
  • When the user validates input such as email address and mobile numbers.

PHP Regex Replace

preg_replace – It is used to perform a pattern match on a string and then replace the matched string with the specified text.

Regex Replace Syntax in PHP

preg_replace ($pattern, $replacement, $string, $limit, &$count)

pattern            Required. Specifies the value to find as pattern
replacement  Required. Specifies the value to replace
string               Required. Specifies the string to be searched

Example :

<?php
$footer_date = "Developer Helps 2018";
$footer_date = preg_replace("([0-9]+)", "2019", $footer_date);
echo $footer_date;
?>

Output :

Developer Helps 2019

Example: Replace all ‘Developer’ with ‘designer’

<?php
$string = "Developer helps is good website for developer.";
$result = preg_replace('/Developer/', 'designer', $string);
print_r($result);
?>

Output :

designer helps is good website for developer.

Example: Replace with case-insensitive matching

<?php
$string = "Developer helps is good website for developer.";
$result = preg_replace('/Developer/i', 'designer', $string);
print_r($result);
?>

Output :

designer helps is good website for designer.

Example : Strip all whitespaces

<?php
$string = "Developer helps is good website for developer.";
$result = preg_replace('/\s+/', '', $string);
print_r($result);
?>

Output :

Developerhelpsisgoodwebsitefordeveloper.

Thanks for reading the post. I hope you like and understand the post. If you have any doubts regarding this post please comment below.

More Related Post

Leave a comment

Your email address will not be published. Required fields are marked *