Home  >  Article  >  Backend Development  >  How to replace keywords in php

How to replace keywords in php

藏色散人
藏色散人Original
2021-03-11 09:17:312474browse

How to replace keywords in php: First create a PHP sample file; then replace the keywords through "str_replace(find,replace,string,count)".

How to replace keywords in php

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

php can be replaced multiple times at one time Keywords

php replace multiple strings str_replace function

echo str_replace(array("m","i"),array("n","z"),"my name is jim!")

Output: ny nane zs jzn!

echo str_replace(array('m','i'),'n',"my name is jim!");
<?php 
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "","Hello World of PHP");
echo $onlyconsonants;
?>
str_replace(find,replace,string,count)
<?php
//把字符串 "Hello world!" 中的字符 "world" 替换为 "Shanghai":

echo str_replace("world","Shanghai","Hello world!");
?>
Parameter Description
find Required. Specifies the value to look for.
replace Required. Specifies the value to replace the value in find.
string Required. Specifies the string to be searched for.
count Optional. Variable counting the number of substitutions.
$arr="abcdefg";
$replace=array(&#39;h&#39;,&#39;i&#39;,&#39;j&#39;,&#39;k&#39;,&#39;l&#39;,&#39;m&#39;,&#39;n&#39;);
$find=array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;);
$str=str_replace($find,$replace,$arr);
var_dump($str);
var_dump($arr);die;

1. Only use arrays for needles.

Example: str_replace(array('m','i'),'n',"my name is jim!");Return: ny nane ns jnn!
It can be seen that the function order Compatibly replaces each string in the array and returns the replaced string.

2. Only use arrays for new_needle.

Example: str_replace('m',array('n','z'),"my name is jim!\n") returns: Arrayy naArraye is jiArray!
This replacement is more interesting. If you only use an array for the second parameter, the function will use it as a string Array, replacing all needles with arrays.

3. Only use arrays for haystack.

Example: str_replace("m","n",array("my name is jim!","the game is over!")) The execution result of this statement returns an array, that is, the incoming The result after replacing the two strings

If you output the array content, you will see: ny nane is jin! the gane is over!

4. Use arrays for both needle and new_needle.

Example: str_replace(array("m","i"),array("n","z"),"my name is jim!")Return: ny nane zs jzn!
Looking at the execution results, you can find that if the first two parameters use arrays, the function replaces the strings of each object item in the array, and replaces the first item of needle with the first item of new_needle. And so on.

If the needle array is longer than new_deedle, for example: str_replace(array("m","i","s"),array("n","z"),"my name is jim!" ); Return: ny nane z jzn! It can be seen that the extra strings in the needle array are replaced with empty strings.
If the new_needle array is longer than needle, for example: str_replace(array("m","i"),array("n","z","x"),"my name is jim!") return ny nane zs jzn! It can be seen that the redundant items of new_needle are ignored.

5. All three parameters use arrays.

例如:str_replace(array("m","i"),array("n","z"),array("my name is jim!","the game is over"))返回的数组内容:ny nane zs jzn!the gane zs over

str_repace function originally supports batch replacement, for example:

// Provides: Hll Wrld f PHP
 $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
 $onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of How to replace keywords 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