Home  >  Article  >  Backend Development  >  Summary of str_replace function examples in php

Summary of str_replace function examples in php

怪我咯
怪我咯Original
2017-07-17 09:20:081562browse

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 e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3 tags in the content field and replace Empty

[@str_replace(array('e388a4556c0f65e1904146cc1a846bee','94b3e26ee717c64999d7867364b1b4a3'), '', $Content)]
One-to-one replacement: I want to enter the content field All 0c6dc11e160d3b678d68754cc175188a tags are replaced with e388a4556c0f65e1904146cc1a846bee

[@str_replace('0c6dc11e160d3b678d68754cc175188a', 'e388a4556c0f65e1904146cc1a846bee', $Content)]
Many-to-many replacement: think Replace 0c6dc11e160d3b678d68754cc175188a in the content field with df250b2156c434f3390392d09b1c9563, replace e388a4556c0f65e1904146cc1a846bee with f32b48428a809b51f04d3228cdf461fa, and clear all 94b3e26ee717c64999d7867364b1b4a3

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn