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, 'P', 'A') . '<br>'; echo strtr($str, 'PP', 'z1') . '<br>'; echo strtr($str, 'P', ' ') . '<br>'; echo strtr($str, 'PH', '12') . '<br>'; echo "<hr>"; ?>
Access through the browser, the result is as follows:
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("中" => '')) . '<br>';
echo strtr($str, array("中" => '.', "文网" => "cn")) . '<br>';
echo "<hr>";
?>Access through the browser, the result is as follows:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.





