search
HomeBackend DevelopmentPHP ProblemHow to replace multiple characters at the same time with php str_replace

Method: Set the first parameter of str_replace() to an array, the syntax "str_replace(array,'replacement value',$str)" can replace multiple characters with the same specified character; If you want to replace it with a different specified character, you only need to set the second parameter to an array, and the number of elements in the two arrays must be the same.

How to replace multiple characters at the same time with php str_replace

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php str_replace simultaneously replace Multiple characters

str_replace() Replaces a specific string specified in the original string with a new string.

str_replace(find,replace,string,count)
Parameters Description
find Required . Specifies the value to look for.
replace Required. Specifies a value that replaces the value in find.
string Required. Specifies the string to be searched for.
count Optional. A variable counting the number of substitutions.

Generally, the str_replace() function only searches for one value and replaces it with the specified value.

<?php
$str = "abchelloabc";
$newStr=str_replace(&#39;a&#39;,&#39;-&#39;,$str);
echo $newStr."<br>";
?>

How to replace multiple characters at the same time with php str_replace

When the first parameter find of the function is set to an array type, multiple values ​​can be searched:

<?php
$str = "abchelloabc";
$newStr=str_replace([&#39;a&#39;,&#39;b&#39;,&#39;c&#39;],&#39;-&#39;,$str);//a或b或c都替换成-
echo $newStr."<br>";
?>

How to replace multiple characters at the same time with php str_replace

This is to replace multiple characters with the same specified character. If the second parameter replace of the function is also set to an array type, it can be replaced with different specified characters

<?php
$str = "abchelloabc";
$newStr=str_replace([&#39;a&#39;,&#39;b&#39;,&#39;c&#39;],[&#39;-&#39;,&#39;!&#39;,&#39;~&#39;],$str);//a->- b->! c->~
echo $newStr."<br>";
?>

How to replace multiple characters at the same time with php str_replace

#Note: At this time, the number of elements in the two arrays must be the same.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to replace multiple characters at the same time with php str_replace. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft