Home > Backend Development > PHP Problem > How to replace a certain character in a string in php

How to replace a certain character in a string in php

王林
Release: 2023-02-28 20:00:01
Original
3715 people have browsed it

How to replace a certain character in a string in php

In PHP, you can use the strtr() function to implement string replacement.

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

Syntax:

string strtr( string $str, string $from, string $to)
Copy after login

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.

Example:

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

The output results are as follows:

How to replace a certain character in a string in php

##For more related tutorials, please pay attention to

php中文网.

The above is the detailed content of How to replace a certain character in a string 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