str_replace() function replaces some characters in a string with other characters (case sensitive).
This function must follow the following rules:
If the searched string is an array, then it will return an array.
If the searched string is an array, then it will find and replace each element in the array.
If you need to search and replace the array at the same time, and the elements that need to be replaced are less than the number of found elements, then the excess elements will be replaced with empty strings
If the searched is an array, and the replacement is a string, then the replacement string will work on all found values.
Usage Example
The following example replaces %body% with black
< php $bodytag = str_replace("%body%", "black", "<body text=%body%>"); echo $bodytag; >
Format:
[@str_replace("Old content to be replaced", "New characters to replace the original content", $Variable name of the replaced content)]
[@str_replace(array('Old1','Old2','Old3'), array('New1','New2','New3'), $Variable name of the replaced content)]
[@str_replace(array('Old1','Old2','Old3' ), 'new content', $Variable name of the replaced content)]
Example:
Many-to-one replacement: I want to clear all
','
'), '', $Content)]
[@str_replace('
', '
', $Content)]
Many-to-many replacement: think Replace
in the content field with
, replace
with
The above is the detailed content of Summary of str_replace function examples in php. For more information, please follow other related articles on the PHP Chinese website!