Publish: 2012-01-02 | Modify: 2018-08-09
Previously, when I was writing PHP programs, I only knew how to use str_replace
to replace a single word. If there were multiple keywords that needed to be replaced in an article, I had to replace them one by one, which was very troublesome. Later, I accidentally discovered that the parameters of str_replace
can be arrays, and it can be written like this:
<?php
$str = "你今天开心不开心呀?";
$arr1 = array("你","开心");
$arr2 = array("他","快乐");
$str = str_replace($arr1, $arr2, $str);
echo $str;
?>
The final output result is: "他今天快乐不快乐呀?" Isn't it convenient to replace words using arrays?
Original article from: php中的str_replace替换数组效率也挺高的, all rights reserved to the original author. If there is any infringement, please contact QQ: 337003006 for deletion.
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.