php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49146 strtr cuts char from the end
Submitted: 2009-08-04 01:36 UTC Modified: 2009-08-04 01:54 UTC
From: juho dot karvilainen at bmf dot wz dot cz Assigned:
Status: Not a bug Package: Output Control
PHP Version: 5.2.10 OS: Win XP
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: juho dot karvilainen at bmf dot wz dot cz
New email:
PHP Version: OS:

 

 [2009-08-04 01:36 UTC] juho dot karvilainen at bmf dot wz dot cz
Description:
------------
looks like the strtr() function (when used with array as 2nd argument) nibbles a character from the end when it has nothing to replace. (on UTF-8 page)

Reproduce code:
---------------
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php
function safe_js($str){
  return strtr($str,array('"'=>'\"',"'">'\'','/'=>'\/'));
}
?>
line1...<?php echo safe_js(480)?>
line2...

Expected result:
----------------
line1...480
line2

Actual result:
--------------
line1...48line2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-04 01:38 UTC] scottmac@php.net
Closing PHP tags always eat the newline after it.
 [2009-08-04 01:54 UTC] scottmac@php.net
Your array is also malformed.

array('"'=>'\"',"'">'\'','/'=>'\/')

array(3) {
  ["""]=>
  string(2) "\""
  [0]=>
  bool(false)
  ["/"]=>
  string(2) "\/"
}
 [2009-08-04 01:54 UTC] felipe@php.net
Note this point: "'">'\''

You mistyped the =>, hence you got a 0-key.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 02:01:35 2025 UTC