Summary of str_replace function examples in php

怪我咯
Release: 2023-03-14 08:16:01
Original
1561 people have browsed it

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; 
>
Copy after login

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

tags in the content field and replace Empty

[@str_replace(array('

','

'), '', $Content)]
One-to-one replacement: I want to enter the content field All
tags are replaced with



[@str_replace('
', '

', $Content)]
Many-to-many replacement: think Replace
in the content field with
, replace

with


, and clear all


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!