PHP has the function to replace all words or letters in a string or sentence with a str_replace() command. This PHP string reference used to replace words in a paragraph on a web page quickly and easily.
Table of Contents
What is str_replace function in PHP?
I quoted from https://www.php.net, str_replace() is a built-in function in PHP that used to replace all search strings with a replacement string.
str_replace()
The supported PHP versions are PHP 4, PHP 5 and PHP 7
Syntax:
1 | str_replace ($search, $replace, $original_string) |
str_replace has 3 parameters. These parameters are requested for the function to work correctly.
$search : the search strings used to find the string that we want to change. This parameter can contain strings or also in the form of Array.
$replace : All words found replaced with this string replace in this parameter.
$original_string : source string or paragraph you want to change.
Following example :
1 2 3 4 | $original_string = "I love pizza"; $search = "pizza"; $replace = "burger"; echo str_replace($search ,$replace, $original_string ); |
With the example above, you have changed the word “pizza” to “burger.” The example above is function to replace a single string.
So what if we want to change multiple replacements at once in one str_replace function? the answer is to use an PHP arrays
PHP Arrays in str_replace()
You can search and replace multiple replacements strings at once using an array. See the following example.
Example 1
We search and replace many strings with a string in a paragraph.
1 2 3 4 | $original_string = "i' love* seegatesite.com~ to get^ any tutorial"; $search = array("'",'*', '~', '^'); $replace = ""; echo str_replace($search ,$replace, $original_string ); |
Example 2
We make 2 arrays, which function as search and replace.
1 2 3 4 | $originalString = "My name is Sigit! I'm programmer who love PHP."; $search = array('programmer','PHP'); $replace = array('Police','Cookies'); echo str_replace($search ,$replace, $originalString ); |
The example above, the searched is an array and the replaced is an array too.
Note:
The str_replace() function is case-sensitive.
If you want to replace words in a string both uppercase and lowercase, you can use the str_ireplace() function. How to use str_ireplace() is the same as str_replace().
str_ireplace()
The supported PHP versions are PHP 5 and PHP 7
Example
1 2 3 4 | $originalString = "I love PizZa" ; $search = 'pizza'; $replace = 'burger'; $result = str_ireplace($search ,$replace, $originalString ); |
Many interesting examples of using PHP strreplace can be found on the PHP official site. One of them is my favorite as follows.
Convert foreign 8859-1 characters into HTML entities
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | /** * Convert foreign 8859-1 characters into HTML entities. * * @param string $str * The string being parsed. * * @return string * The converted string. */ public static function convert_chars_to_entities( $str ) { $str = str_replace( 'À', 'À', $str ); $str = str_replace( 'Á', 'Á', $str ); $str = str_replace( 'Â', 'Â', $str ); $str = str_replace( 'Ã', 'Ã', $str ); $str = str_replace( 'Ä', 'Ä', $str ); $str = str_replace( 'Å', 'Å', $str ); $str = str_replace( 'Æ', 'Æ', $str ); $str = str_replace( 'Ç', 'Ç', $str ); $str = str_replace( 'È', 'È', $str ); $str = str_replace( 'É', 'É', $str ); $str = str_replace( 'Ê', 'Ê', $str ); $str = str_replace( 'Ë', 'Ë', $str ); $str = str_replace( 'Ì', 'Ì', $str ); $str = str_replace( 'Í', 'Í', $str ); $str = str_replace( 'Î', 'Î', $str ); $str = str_replace( 'Ï', 'Ï', $str ); $str = str_replace( 'Ð', 'Ð', $str ); $str = str_replace( 'Ñ', 'Ñ', $str ); $str = str_replace( 'Ò', 'Ò', $str ); $str = str_replace( 'Ó', 'Ó', $str ); $str = str_replace( 'Ô', 'Ô', $str ); $str = str_replace( 'Õ', 'Õ', $str ); $str = str_replace( 'Ö', 'Ö', $str ); $str = str_replace( '×', '×', $str ); $str = str_replace( 'Ø', 'Ø', $str ); $str = str_replace( 'Ù', 'Ù', $str ); $str = str_replace( 'Ú', 'Ú', $str ); $str = str_replace( 'Û', 'Û', $str ); $str = str_replace( 'Ü', 'Ü', $str ); $str = str_replace( 'Ý', 'Ý', $str ); $str = str_replace( 'Þ', 'Þ', $str ); $str = str_replace( 'ß', 'ß', $str ); $str = str_replace( 'à', 'à', $str ); $str = str_replace( 'á', 'á', $str ); $str = str_replace( 'â', 'â', $str ); $str = str_replace( 'ã', 'ã', $str ); $str = str_replace( 'ä', 'ä', $str ); $str = str_replace( 'å', 'å', $str ); $str = str_replace( 'æ', 'æ', $str ); $str = str_replace( 'ç', 'ç', $str ); $str = str_replace( 'è', 'è', $str ); $str = str_replace( 'é', 'é', $str ); $str = str_replace( 'ê', 'ê', $str ); $str = str_replace( 'ë', 'ë', $str ); $str = str_replace( 'ì', 'ì', $str ); $str = str_replace( 'í', 'í', $str ); $str = str_replace( 'î', 'î', $str ); $str = str_replace( 'ï', 'ï', $str ); $str = str_replace( 'ð', 'ð', $str ); $str = str_replace( 'ñ', 'ñ', $str ); $str = str_replace( 'ò', 'ò', $str ); $str = str_replace( 'ó', 'ó', $str ); $str = str_replace( 'ô', 'ô', $str ); $str = str_replace( 'õ', 'õ', $str ); $str = str_replace( 'ö', 'ö', $str ); $str = str_replace( '÷', '÷', $str ); $str = str_replace( 'ø', 'ø', $str ); $str = str_replace( 'ù', 'ù', $str ); $str = str_replace( 'ú', 'ú', $str ); $str = str_replace( 'û', 'û', $str ); $str = str_replace( 'ü', 'ü', $str ); $str = str_replace( 'ý', 'ý', $str ); $str = str_replace( 'þ', 'þ', $str ); $str = str_replace( 'ÿ', 'ÿ', $str ); return $str; } |
Another tutorial : How To Send And Retrieve JSON Data PHP CURL Post Example
Conclusion
str_replace() is built-in string functions to replace word in string rapidly. instead of using string variables, you can do multiple search strings using array and replace with another string.
You don’t need to bother making regular expressions to replace word in strings.
Thus my short tutorial on how to use PHP string replace, hopefully, useful
Leave a Reply