php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22732 eregi_replace function does not find all appearences in a string
Submitted: 2003-03-15 18:29 UTC Modified: 2003-03-16 06:26 UTC
From: ofjord at simnet dot is Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.3.0 OS: windows nt
Private report: No CVE-ID: None
 [2003-03-15 18:29 UTC] ofjord at simnet dot is
I want to parse a HTML document to replace all links to .htm documents so that the .htm document becomes a parameter in a php script.

Here is the code to replace the links:

  $text = eregi_replace("href=(\")?(.*[.]htm)(\"?)", "href=\\1template1.php?file=\\2\\3", $text);

This works fine, but only once in each document. This text:

<B>&#149;</B> <a href="ferdatjon_fyrirt_gisting.htm">Gisting</a><BR><BR>
<B>&#149;</B> <a href="ferdatjon_fyrirt_handverkshus.htm">Handverksh?s</a><BR><BR>

returns:

<B>&#149;</B> <a href="template1.php?file=ferdatjon_fyrirt_gisting.htm">Gisting</a><BR><BR>
<B>&#149;</B> <a href="ferdatjon_fyrirt_handverkshus.htm">Handverksh?s</a><BR><BR>

Is this a bug?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-15 20:33 UTC] pollita@php.net
Actually, it *is* expected behavior.  Your pattern is too greedy and is actually matching:

ferdatjon_fyrirt_gisting.htm">Gisting</a><BR><BR>
<B>&#149;</B> <a
href="ferdatjon_fyrirt_handverkshus.htm

as your (.*[.]htm) pattern (see how that does match?)

For a solution (making your pattern less greedy), send an email to php-general@lists.php.net which is the proper forum for this type of question.  Alternatively you could do some online or book research into building regex patterns.
 [2003-03-16 06:26 UTC] ofjord at simnet dot is
Thank you for your advice. Now my regex code looks like this and works perfectly:

  $text = eregi_replace("href=(\")?([_/a-zA-Z0-9]*[.]s?html?)(\"?)", "href=\\1template1.php?file=\\2\\3", $text);
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 17:01:33 2025 UTC