Home  >  Article  >  Backend Development  >  PHP中str_replace的指定替换次数不生效

PHP中str_replace的指定替换次数不生效

WBOY
WBOYOriginal
2016-06-20 12:29:441195browse

<?php header("Content-Type: text/html; charset=utf-8");$str="hello world he,he,he,he,he,he";echo str_replace("he", "替换", $str,$count=1);?>

指定替换次数为一次,  但是结果是被全部替换,  php版本是5.4


回复讨论(解决方案)

你理解错了第四个参数,它是计算一共替换了多少次,而不是限制替换的次数

str_replace("he", "替换", $str,$count);
$count是返回被替换的次数

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