php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24645 ereg_replace fails to strtolower replaced string
Submitted: 2003-07-14 08:37 UTC Modified: 2003-07-14 12:00 UTC
From: nek at capsule dot org Assigned:
Status: Not a bug Package: Regexps related
PHP Version: 4.3.1 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: nek at capsule dot org
New email:
PHP Version: OS:

 

 [2003-07-14 08:37 UTC] nek at capsule dot org
Description:
------------
Hi,

i'm trying to replace a portion of string by its strtolowered equivalent

see reproduce code and results below : it should give "href=javascript:comment()" but only "HREF" is lowered, not the matching string \1



Reproduce code:
---------------
<?php
$newsaff = "<A HREF=\"JAVASCRIPT:COMMENT()\">THIS IS A TEST LINK</A>";

$newsaff = ereg_replace("HREF=\"([^\"]+)\"",strtolower("HREF=\"\\1\""),$newsaff);

print $newsaff;
?>

Expected result:
----------------
<A href="javascript:comment()">THIS IS A TEST LINK</A>

Actual result:
--------------
<A href="JAVASCRIPT:COMMENT()">THIS IS A TEST LINK</A>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-14 08:39 UTC] nek at capsule dot org
sorry, it's php 4.3.1 and not 4.3.2
 [2003-07-14 09:09 UTC] b_ulrich at t-online dot de
Using strtolower("HREF=\"\\1\"") as parameter is like using "href=\"\\1\"" directly.
First the strtolower is processed and then that result is used as parameter.

You have to change your search/replace pattern to get what you want.

The function works correct.
 [2003-07-14 09:19 UTC] nek at capsule dot org
well,

maybe the matching string should be considered as a normal one, so we can use some functions on it.

this would render ereg_replace function even more powerfull, isn't it ?
 [2003-07-14 09:21 UTC] jay@php.net
That's correct, so this is bogus. Try using 
ereg()/preg_match() with the optional registers used in 
the third argument to get the results you're looking for. 
 
J 
 [2003-07-14 09:39 UTC] nek at capsule dot org
you mean something like :

<?php
$newsaff = "<A HREF=\"JAVASCRIPT:COMMENT()\">THIS IS A TEST LINK</A>";

$newsaff =
ereg("HREF=\"([^\"]+)\"",$newsaff,$newsreplace);

for ($i=1;$i<sizeof($newreplace);$i++)

print $newsaff;
?>
 [2003-07-14 09:43 UTC] nek at capsule dot org
doh... tab then return = submit form.. sorry

i said :

<?php
$newsaff = "<A HREF=\"JAVASCRIPT:COMMENT()\">THIS IS A TEST LINK</A>";

$newsaff =
ereg("HREF=\"([^\"]+)\"",$newsaff,$newsreplace);

for ($i=1;$i<sizeof($newreplace);$i++) {
 $newsaff = str_replace($newreplace[$i],strtolower($newreplace[$i]),$newsaff);
 }

print $newsaff;
?>

maybe there's a simplier solution, that's coded from scratch ;)

but yes indeed, that's complicated and a non bogus ereg_replace would be from great help :)

thanks
 [2003-07-14 10:10 UTC] jay@php.net
ereg_replace() is working exactly as expected. The 
inner-most functions are processed first, so strtolower() 
gets processed before ereg_replace(). Therefore, 
"HREF=\"\\1\"" is being sent through strtolower(), not "<A 
HREF=\"JAVASCRIPT:COMMENT()\">". strtolower() has no idea 
what \1 means other than \1. This is totally expected 
behaviour.  
 
Try doing something writing like foo(bar($s)) and write 
your own functions for foo() and bar(). bar() is always 
performed first. Same thing in this case.  
 
The behaviour you're proposing is just not the expected 
bevariour, hence the bogusness. 
 
J 
 [2003-07-14 12:00 UTC] nek at capsule dot org
oups,

sorry, i realize now that \1 will be replaced by ereg_replace, but is not yet replaced inside the function

show get some sleep or vitamin i guess
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Nov 24 04:01:23 2020 UTC