search
HomeDaily ProgrammingPHP KnowledgeHow to replace a specified string in PHP? (Pictures + Videos)

PHP replaces the specified string , which is usually asked as a basic knowledge point during the PHP interview process. Then in PHP, there are two functions that can implement string replacement, strtr() and str_repalce() functions.

First of all, let’s briefly understand the definition and syntax of the strtr() function.

strtr: Convert specified characters.

Two syntaxes:

The first syntax:

string strtr( string $str, string $from, string $to)

The first parameter represents the string to be converted . The second parameter represents the source character in the string corresponding to the destination character to to be converted. The third parameter represents the destination character in the string corresponding to the character from to be converted. ​

Second syntax:

string strtr( string $str , array $replace_pairs )

The first parameter represents the string to be converted. The second parameter represents replacing the specified string in the form of an array key value.

Below we will combine two simple code examples to introduce in detail the usage and usage techniques of the most commonly used strtr() function.

Strtr function in PHP replaces string example 1:

<?php
$str = "PHP中文网";
echo strtr($str, &#39;P&#39;, &#39;A&#39;) . &#39;<br>&#39;;
echo strtr($str, &#39;PP&#39;, &#39;z1&#39;) . &#39;<br>&#39;;
echo strtr($str, &#39;P&#39;, &#39; &#39;) . &#39;<br>&#39;;
echo strtr($str, &#39;PH&#39;, &#39;12&#39;) . &#39;<br>&#39;;
echo "<hr>";
?>

Access through the browser, the result is as follows:

How to replace a specified string in PHP? (Pictures + Videos)

You can combine the code and return value to understand the rules for replacing strings with the strtr function syntax.

Some friends here may be a little confused about the result of replacing "z1" with "PP" in the second line. This is because if you want to convert two identical destination characters, the value of the last character is used as the standard. . Since the last "P" here corresponds to "1", the replacement result is converted from "PHP Chinese Network" to "1H1 Chinese Network".

The third line shows the result of replacing the space character, which is also very easy to understand. In other cases, just replace the corresponding characters.

Strtr function in PHP replaces string example 2:

<?php
$str = "PHP中文网";
echo strtr($str, array("中" => &#39;&#39;)) . &#39;<br>&#39;;
echo strtr($str, array("中" => &#39;.&#39;, "文网" => "cn")) . &#39;<br>&#39;;
echo "<hr>";
?>

Access through the browser, the result is as follows:

How to replace a specified string in PHP? (Pictures + Videos)

As shown in the figure, Chinese string replacement can also be achieved using the strtr function in PHP.

This article is about the specific method of PHP replacing the specified string. Using the strtr function can not only replace multiple strings but also replace Chinese strings, etc.

If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How to replace a specified string in PHP? (Pictures + Videos). 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.