php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52024 strtr behaves strangely with multi bytes strings
Submitted: 2010-06-08 17:29 UTC Modified: 2010-10-23 02:12 UTC
From: francois dot gannaz at silecs dot info Assigned: cataphract (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.2 OS: Linux Debian testing amd64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: francois dot gannaz at silecs dot info
New email:
PHP Version: OS:

 

 [2010-06-08 17:29 UTC] francois dot gannaz at silecs dot info
Description:
------------
strtr() only translates _bytes_, not characters.
When translating a multi-byte character to a single byte character, only the first byte is converted, resulting in a broken string.
The examples given in the doc work with an ISO-8859-1 encoding of the source file, but not with UTF-8.

IMHO, two solutions;
1) Fix the function. But then the function would need to guess the file encoding. Hmmm....PHP6?
2) Fix the doc.
   The doc title, "strtr — Translate certain characters", is misleading. Either replace "characters" by "bytes", or add somewhere on the page that it only works on single-byte encodings. A big warning would be useful.

Test script:
---------------
// in an UTF-8 file
echo strtr("’", "’", "'");
echo strtr("äåö", "äåö", "aao"); // example from php.net

Expected result:
----------------
'aao

Actual result:
--------------
'��oao�o�

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-22 15:47 UTC] kalle@php.net
-Package: Strings related +Package: Documentation problem
 [2010-10-23 02:12 UTC] cataphract@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cataphract
 [2010-10-23 02:12 UTC] cataphract@php.net
I've already improved the documentation of strtr for bug #52967.

Basically, strtr() has two different algorithms implemented under the same name, in one case it translates bytes, in the other one it replaces strings.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 20:00:02 2026 UTC