Bug #75172 [Com]: $replace_pairs can't be used instead of $to and $from
| From: | kelunik@php.net | Date: | Fri, 08 Sep 2017 10:33:48 +0000 |
| Subject: | Bug #75172 [Com]: $replace_pairs can't be used instead of $to and $from | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-211007@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75172&edit=1
ID: 75172
Comment by: kelunik@php.net
Reported by: hyman dot omg at gmail dot com
Summary: $replace_pairs can't be used instead of $to and
$from
Status: Not a bug
Type: Bug
Package: Strings related
Operating System: centos6.5
PHP Version: 5.6.31
Block user comment: N
Private report: N
New Comment:
These are replace pairs, see https://3v4l.org/3rfuV
<?php
$str = 'I loved you';
$from = 'love';
$to = 'lOvE';
var_dump($result1 = strtr($str, $from, $to));
var_dump($result2 = strtr($str, [$from[0] => $to[0], $from[1] => $to[1], $from[2] =>
$to[2], $from[3] => $to[3]]));
var_dump($result2 = strtr($str, [$from => $to]));
var_dump($result1===$result2);//false
Previous Comments:
------------------------------------------------------------------------
[2017-09-08 10:33:07] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Please read all the docs, not just parts of it.
> If given three arguments, this function returns a copy of str where all
> occurrences of each (single-byte) character in from have been translated to the
> corresponding character in to
> 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.
That is, strtr has two different behaviors: character replacement by passing two strings or string
replacement by passing one array.
------------------------------------------------------------------------
[2017-09-08 10:25:42] hyman dot omg at gmail dot com
Description:
------------
In PHP Manual,we can find the message "The replace_pairs parameter may be used instead of to
and from, in which case it's an array in the form array('from' => 'to',
...)".In fast,the results are different,it doesn't work as documented.
Test script:
---------------
<?php
$str = 'I loved you';
$from = 'love';
$to = 'lOvE';
var_dump($result1 = strtr($str, $from, $to));//I lOvEd yOu
var_dump($result2 = strtr($str, [$from=>$to]));//I lOvEd you
var_dump($result1===$result2);//false
Expected result:
----------------
I lOvEd you
I lOvEd you
true
Actual result:
--------------
I lOvEd yOu
I lOvEd you
false
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75172&edit=1