php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20221 str_replace
Submitted: 2002-11-02 13:04 UTC Modified: 2003-06-24 11:49 UTC
From: panacode at skynet dot be Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.2.3 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: panacode at skynet dot be
New email:
PHP Version: OS:

 

 [2002-11-02 13:04 UTC] panacode at skynet dot be
str_replace when used with with arrays,
for every element in the search/replace arrays
a simple replace in the source string (rather than
using an external string) is performed.

This causes an unexpected result like
if one of the replace values includes
a search value it will be replaced in a subsequent
replacing action.

If this is a feature rather than a bug (which I doubt)
please state it in the documentation.

An example:

$vSearch[] = '@gill';
$vSearch[] = '@doubleyou';

$vReplace[] = '@doubleyou';
$vReplace[] = '@bates';

$sSubject = "@gill is my friend";
$sResult = str_replace($vSearch, $vReplace, $sSubject);

echo $sResult; 
// will output "@bates is not my friend" instead
// of "@doubleyou is not my friend"



Best regards,
Eugen Fernea
IT Manager
Panacode Software
rue de la Station, 1/1
7090 Braine-Le-Comte
Belgium
E-mail: panacode@skynet.be
Phone: +32 067/48 58 94
Mobile: +32 (0)472 95 15 48
Web: http://www.panacode.com

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-02 14:22 UTC] kalowsky@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-latest.zip


 [2002-11-02 17:43 UTC] panacode at skynet dot be
Sorry for bothering (and for my writing errors).

There actually is another function in PHP that has the
behaviour that I expect: strtr(str, arr)

$vReplace['@gill'] = '@doubleyou';
$vReplace['@doubleyou'] = '@bates';

$sSubject = "@gill @doubleyou is not my friend";
$sResult = strtr($sSubject, $vReplace);

echo $sResult; 
// will output "@doubleyou @bates is not my friend"
 [2002-11-02 17:46 UTC] iliaa@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.


 [2003-06-19 16:29 UTC] heinz at hhombergs dot de
Sorry but there is a bug in the str_replace function. Reproduced under 4.2.2 and 4.3.2

function recode_charset($string,$cs_from,$cs_to) {
  $trans_tbl["ASCII"] = array("&", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?");

  $trans_tbl["UNICODE"] = array("&", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ");

  $trans_tbl["HTML"] = array("&", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ");

  return str_replace($trans_tbl[$cs_from],  $trans_tbl[$cs_to],  $string);
}

$string = recode_charset('???????','ASCII','HTML');

will produce ÃãÛûÕõÞ

but ÄäÜüÖöß is correct.
 [2003-06-20 10:09 UTC] jay@php.net
In php_str_replace_in_subject(), if a value in the search 
array is of 0 length, the search array internal pointer is 
moved ahead, but not the replace array. Shouldn't they 
both be moved ahead? This would produce the results 
panacode is expecting, but it would break BC, but I don't 
see any documentation that covers this behaviour. Unless 
I'm blind, of course, which I may be. 
 
J 
 [2003-06-24 11:39 UTC] heinz at hhombergs dot de
But as you can see the search and the replace array in the function have no 0 length values and all three arrays are of the same size.
 [2003-06-24 11:49 UTC] jay@php.net
Really? Then what is the value of $trans_tbl['ASCII'][13]? 
That looks zero-length to me.  
 
php_str_replace_in_subject() previously moved the search 
array forward and didn't move the replace array. This has 
since been fixed in CVS. 
 
J 
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 03:01:27 2019 UTC