php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50570 str_replace doesn't work with URL replacement
Submitted: 2009-12-24 15:57 UTC Modified: 2009-12-24 16:12 UTC
From: julijan dot andjelic at gmail dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.3.1 OS: Linux Mint 7 (2.6.28-17-generic)
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: julijan dot andjelic at gmail dot com
New email:
PHP Version: OS:

 

 [2009-12-24 15:57 UTC] julijan dot andjelic at gmail dot com
Description:
------------
When using str_replace with 2 arrays and 1 of them contains URL-s the result gets messed up. I provided a code snippet which shows it.

Reproduce code:
---------------
<?php

$str='I love a b c';

$array0=array('a','b','c');
$array1=array('http://www.example.com/1.html','http://www.example.com/2.html','http://www.example.com/3.html');

echo str_replace($array0, $array1, $str);

?>

Expected result:
----------------
I love http://www.example.com/1.html http://www.example.com/2.html http://www.example.com/3.html

Actual result:
--------------
I love http://www.example.http://www.example.com/3.htmlom/1.html http://www.example.http://www.example.com/3.htmlom/2.html http://www.example.com/3.html

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-24 16:12 UTC] johannes@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

Givenyourcode it will take the string

$str=\'I love a b c\';

And then replace \'a\' with \'http://www.example.com/1.html\' this gives

$str=\'I love http://www.example.com/1.html b c\';

then it will replace \'b\' with \'http://www.example.com/2.html\' so the result is 

$str=\'I love http://www.example.com/1.html http://www.example.com/2.html c\';

Finally it will replace \'c\' with http://www.example.com/3.html, mind that both URLs include a \'c\' in \'.com\' that will be replaced, too.

so the result is

I love http://www.example.http://www.example.com/3.htmlom/1.html http://www.example.http://www.example.com/3.htmlom/2.html http://www.example.com/3.html


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 09:01:29 2024 UTC