php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75172 $replace_pairs can't be used instead of $to and $from
Submitted: 2017-09-08 10:25 UTC Modified: 2017-09-08 10:33 UTC
From: hyman dot omg at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.6.31 OS: centos6.5
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: hyman dot omg at gmail dot com
New email:
PHP Version: OS:

 

 [2017-09-08 10:25 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-08 10:33 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-09-08 10:33 UTC] 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:33 UTC] kelunik@php.net
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
 [2017-09-08 10:33 UTC] mail at pmmaga dot net
You are misreading the docs. It states:

> 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.

It says, "of each character". So all your lowercase "o" and "e" will be uppercased in your example.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC