Str_Replace 不调用函数
Str_Replace Without Recalling the Function
我如何在 PHP 中使用 str_replace 而不允许它重新替换以下代码中 $replace_with 中使用的字符?
<?php
$string='abcd';
$replace= array('a','c');
$replace_with = '[ac]';
$string=str_replace($replace,$replace_with,$string);
echo $string;
?>
结果将是:
"[a[ac]]b[ac]d"
如何强制我的脚本将 $string 中的 'a' 替换为 '[ac]' 一次,而不允许它替换新插入的'[ac]'?
里面的'c'
strtr()
正是您所要求的:
$string = strtr( $string, array( 'a'=>'[ac]', 'c'=>'[ac]' ) );
输出:
[ac]b[ac]d
相关摘录:
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 中使用 str_replace 而不允许它重新替换以下代码中 $replace_with 中使用的字符?
<?php
$string='abcd';
$replace= array('a','c');
$replace_with = '[ac]';
$string=str_replace($replace,$replace_with,$string);
echo $string;
?>
结果将是:
"[a[ac]]b[ac]d"
如何强制我的脚本将 $string 中的 'a' 替换为 '[ac]' 一次,而不允许它替换新插入的'[ac]'?
strtr()
正是您所要求的:
$string = strtr( $string, array( 'a'=>'[ac]', 'c'=>'[ac]' ) );
输出:
[ac]b[ac]d
相关摘录:
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.