PHP str_replace () function example tutorial
Definition and usage
This str_replace () function replaces some characters with other character strings.
This function works in the following rules:
If the string is searched as an array, it returns an array
If the string is searched for as an array, the search and replacement use each array element.
If you find and replace the array, and replace a small part of it than finding an empty string, it will be used as a replacement
If an array is found and replaced by a string, replacement of the string will be used for every search for value.
Syntax
str_replace(find,replace,string,count)
| Parameter |
Description |
| Find |
Required. Specified value discovery |
| Replace |
Required. Specified value to replace value discovery |
| String |
Required. The specified string is searched |
| Count |
Optional. Replace the count of a Variable |
Tips and instructions
Note: This function is case sensitive. Use str_ireplace () to perform a case-insensitive search.
Note: This function is binary secure.
--------------------------------------------------------------------------------
Example 1
<?phpecho str_replace("world","Peter","Hello world!");?>
Output result.
Hello Peter!
Example 2
In this example, we will show the same array of str_replace () and a counting variable:
<?php$arr = array("blue","red","green","yellow");print_r(str_replace("red","pink",$arr,$i));echo "Replacements: $i";?> |
Output result
Array([0] => blue[1] => pink[2] => green[3] => yellow)Replacements: 1