Home  >  Article  >  Backend Development  >  How to batch replace strings in php

How to batch replace strings in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-06 12:02:456243browse

php method to replace strings in batches: use [str_replace] to find and replace strings in batches, the code is [$str = str_replace('o','O',$str,$count);echo $str .PHP_EOL;].

How to batch replace strings in php

php method to replace strings in batches:

str_replace Batch search and replace strings

<?php
$str = &#39;I Love You!&#39;;
$str = str_replace(&#39;o&#39;,&#39;O&#39;,$str,$count);
echo $str.PHP_EOL; // I LOve YOu!
echo &#39;替换了&#39;.$count.&#39;个&#39;.PHP_EOL; // 替换了2个
$str = &#39;I Love You!&#39;;
$str = str_replace([&#39;o&#39;,&#39;u&#39;],&#39;O&#39;,$str,$count);
echo $str.PHP_EOL; // I LOve YOO!
echo &#39;替换了&#39;.$count.&#39;个&#39;.PHP_EOL; // 替换了3个

Related free learning recommendations: php programming (video)

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