简体   繁体   English

PHP使用str_replace替换多个值?

[英]PHP replace multiple value using str_replace?

I need to replace multiple value using str_replace. 我需要使用str_replace替换多个值。

This is my PHP code fore replace. 这是我的PHP代码替换。

$date = str_replace(
       array('y', 'm', 'd', 'h', 'i', 's'),
       array('Year', 'Month', 'Days', 'Hours', 'Munites', 'Seconds'),
       $key
    );

When i pass m in $key it return output like. 当我在$key传递m时,返回输出就像。

MontHours

When i pass h in $key it return output. 当我在$key传递h时它返回输出。

HourSeconds

it return this value i want the Month only. 它返回这个值我只想要Month

Why doesn't it work? 为什么不起作用?

This is a replacement gotcha which is mentioned in the documentation for str_replace() : 这是替换问题,在str_replace()文档中提到:

Replacement order gotcha 更换订单了

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. 因为str_replace()从左向右替换,所以在执行多次替换时,它可能会替换先前插入的值。 See also the examples in this document. 另请参阅本文档中的示例。

Your code is equivalent to: 您的代码相当于:

$key = 'm';

$key = str_replace('y', 'Year', $key);
$key = str_replace('m', 'Month', $key);
$key = str_replace('d', 'Days', $key);
$key = str_replace('h', 'Hours', $key);
$key = str_replace('i', 'Munites', $key);
$key = str_replace('s', 'Seconds', $key);

echo $key;

As you can see m gets replaced with Month , and h in Month gets replaced with Hours and the s in Hours gets replaced with Seconds . 正如你所看到的m被替换为Month ,并hMonth被替换为HourssHours被替换为Seconds The problem is that when you're replacing h in Month , you're doing it regardless of whether the string Month represents what was originally Month or what was originally an m . 问题是当你在Month替换h时, 无论字符串Month表示最初的Month还是原来的m你都会这样做 Each str_replace() is discarding some information — what the original string was. 每个str_replace()都会丢弃一些信息 - 原始字符串是什么。

This is how you got that result: 这就是你得到这个结果的方式:

0) y -> Year
Replacement: none

1) m -> Month
Replacement: m -> Month

2) d -> Days
Replacement: none

3) h -> Hours
Replacement: Month -> MontHours

4) i -> Munites
Replacement: none

5) s -> Seconds
Replacement: MontHours -> MontHourSeconds

The solution 解决方案

The solution would be to use strtr() because it won't change already replaced characters. 解决方案是使用strtr()因为它不会更改已经替换的字符。

$key = 'm';
$search = array('y', 'm', 'd', 'h', 'i', 's');
$replace = array('Year', 'Month', 'Days', 'Hours', 'Munites', 'Seconds');

$replacePairs = array_combine($search, $replace);
echo strtr($key, $replacePairs); // => Month

From the manual page for str_replace() : str_replace()的手册页:

Caution 警告

Replacement order gotcha 更换订单了

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. 因为str_replace()从左向右替换,所以在执行多次替换时,它可能会替换先前插入的值。 See also the examples in this document. 另请参阅本文档中的示例。

For example, "m" is replaced with "Month", then the "h" in "Month" is replaced with "Hours", which comes later in the array of replacements. 例如,“m”替换为“Month”,则“Month”中的“h”将替换为“Hours”,后者将在替换数组中出现。

strtr() doesn't have this issue because it tries all keys of the same length at the same time: strtr()没有这个问题,因为它同时尝试所有相同长度的键:

$date = strtr($key, array(
    'y' => 'Year',
    'm' => 'Month',
    'd' => 'Days',
    'h' => 'Hours',
    'i' => 'Munites', // [sic]
    's' => 'Seconds',
));

A simpler fix is to change the search order: 更简单的解决方法是更改​​搜索顺序:

array('Year', 'Seconds', 'Hours', 'Month', 'Days', 'Minutes')

str_replace and preg_replace will both search for each search item one at a time. str_replacepreg_replace将一次搜索一个搜索项。 Any multi-value will need to make sure the order doesn't change a previous replace item. 任何多值都需要确保订单不会更改先前的替换项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM