gpt4 book ai didi

php - PHP 的 str_replace 替代方案

转载 作者:行者123 更新时间:2023-12-04 21:49:34 28 4
gpt4 key购买 nike

str_replace manual对于 PHP,它声明如下:

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.



是否有没有此问题的等效功能,或者我如何安全地执行此操作?

最佳答案

您正在寻找 strtr ( string $str , array $replace_pairs ) .

If given two arguments, the second should be an array in the form array('from' => 'to', ...). The return value is a string where all the occurrences of the array keys have been replaced by the corresponding values. The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.



手册中的示例:
<?php
$trans = array("h" => "-", "hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
?>

将输出:

hello all, I said hi



这应该正是您所需要的。

关于php - PHP 的 str_replace 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9721697/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com