php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41235 ereg_replace does not replace all occurrences
Submitted: 2007-04-30 06:47 UTC Modified: 2010-12-29 14:02 UTC
From: alexey at gmail dot com Assigned:
Status: Not a bug Package: Regexps related
PHP Version: 4.4.6 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
11 + 41 = ?
Subscribe to this entry?

 
 [2007-04-30 06:47 UTC] alexey at gmail dot com
Description:
------------
ereg_replace() does not replace all occurrences of a regular expression as the following example shows. The pattern

&([^;][^;][^;][^;][^;])

is replaced with 

&\\1

in a string

http://search.networkworld.com/index.html?col=&qs=&qc=&si=0&ql=a&nh=25&lk=1&rf=0qqqqqqqqqqqq

The output is

http://search.networkworld.com/index.html?col=&qs=&qc=&si=0&ql=a&nh=25&lk=1&rf=0qqqqqqqqqqqq

which indicates for example that the last & was not replaced.

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

$data = "http://search.networkworld.com/index.html?col=&qs=&qc=&si=0&ql=a&nh=25&lk=1&rf=0qqqqqqqqqqqq";

$pattern="&([^;][^;][^;][^;][^;])";
$data=ereg_replace($pattern, "&amp;\\1", $data);
echo $data."\n";

// http://search.networkworld.com/index.html?col=&amp;qs=&qc=&amp;si=0&ql=a&amp;nh=25&amp;lk=1&rf=0qqqqqqqqqqqq

$data=ereg_replace($pattern, "&amp;\\1", $data);
echo $data."\n";

// http://search.networkworld.com/index.html?col=&amp;qs=&amp;qc=&amp;si=0&amp;ql=a&amp;nh=25&amp;lk=1&amp;rf=0qqqqqqqqqqqq

?>


Expected result:
----------------
http://search.networkworld.com/index.html?col=&amp;qs=&amp;qc=&amp;si=0&amp;ql=a&amp;nh=25&amp;lk=1&amp;rf=0qqqqqqqqqqqq

Actual result:
--------------
http://search.networkworld.com/index.html?col=&amp;qs=&qc=&amp;si=0&ql=a&amp;nh=25&amp;lk=1&rf=0qqqqqqqqqqqq

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-30 09:33 UTC] robinv at gmail dot com
Not a bug:

The pattern matches '&qs=&q' and replaces it with '&amp;qs=&q'. It matches '&si=0&q' and replaces it with '&amp;si=0&q'. It matches '&nh=25' and replaces it with '&amp;nh=25' and it matches '&lk=1&' replacing it with '&amp;lk=1&'.

Which is exactly what you've told it to do.
 [2007-04-30 11:05 UTC] alexey at gmail dot com
Oh well. Thanks for your explanation. Closed.
 [2007-04-30 11:15 UTC] alexey at gmail dot com
The PHP manual does not specify the replacement order. What if we replace it from right to the left? This will give us a different answer. Also, the definition of a regular expression does not impose the evaluation order. Therefore, the right answer is to replace each occurrence of & in the original string with a &amp;.
 [2010-12-29 14:02 UTC] jani@php.net
-Package: *Search functions +Package: Regexps related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC