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

How to replace a certain character in a string in php

王林
王林Original
2020-04-25 15:42:393644browse

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)

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>";
?>

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!

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