PHP String Replace Now we arrive in 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.
PHP String Replace Syntax
str_replace (find, replace, string, count)
PHP Replace Explanation
1.) find Required. Specifies the value to find
2.) replace Required. Specifies the value to replace
3.) string Required. Specifies the string to be searched
4.) count Optional. A variable that counts the number of replacements
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 Occurrence of “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 parametrs in which you can put a number who tell 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 replace.
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 is a Regular Expressions?
Regular expressions are pattern matching algorithm that can be performed in a single expression.
For creating a complex expression, Regular expressions use arithmetic operators such as (+,-,^).
Why to use regular expressions ?
- Regular expressions can be used to identify the patterns in the string by calling a single function.
- When user validates input such as email address, 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
# 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.
# 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.
# Strip all whitespaces
<?php
$string = "Developer helps is good website for developer.";
$result = preg_replace('/\s+/', '', $string);
print_r($result);
?>
Output
Developerhelpsisgoodwebsitefordeveloper.
Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.