|  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:
Solve the problem:
41 + 48 = ?
Subscribe to this entry?

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


is replaced with 


in a string

The output is

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

Reproduce code:

$data = "";

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


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



Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


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]
-Package: *Search functions +Package: Regexps related
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Feb 25 05:01:26 2024 UTC