php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #21816 preg_replace has problem with arrays
Submitted: 2003-01-22 07:43 UTC Modified: 2003-01-22 19:09 UTC
From: grischa dot toedt at web dot de Assigned: pollita (profile)
Status: Closed Package: Documentation problem
PHP Version: 4.3.0 OS: Windows XP
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: grischa dot toedt at web dot de
New email:
PHP Version: OS:

 

 [2003-01-22 07:43 UTC] grischa dot toedt at web dot de
<?php
// Hi, while using the following code i experienced
// an interesting behaviour of preg_replace():

$string = "Say a [word0] about [word1] and [word2]";

$search[0] ="[word0]";
$search[1]= "[word1]";
$search[2]= "[word2]";

$replace[0]="betterWord0";
$replace[2]="betterWord2";
$replace[1]="betterWord1";

$string = preg_replace($search,$replace,$string);

echo "Result is:<BR>" . $string;
echo "<BR><BR>Result should be:<BR> Say a [betterWord0] about [betterWord1] and [betterWord2]";

// result should be:
//Say a [betterWord0] about [betterWord1] and [betterWord2]
//
// but produces: 
//Say a [betterWord0] about [betterWord2] and [betterWord1]
//
// Seems like the order in which i build my array
// is the order for replacing...and not the index
// of $array[$index]
//
//
// Has someone an idea, about what's happening here?
//
// just a guy addicted to php ;-)
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-22 08:41 UTC] grischa dot toedt at web dot de
<?php
// Hi, while using the following code i experienced
// an interesting behaviour of preg_replace():

$string = "Say a [word0] about [word1] and [word2]";

$search[0] ="[word0]";
$search[1]= "[word1]";
$search[2]= "[word2]";

$replace[0]="betterWord0";
$replace[2]="betterWord2";
$replace[1]="betterWord1";

$string2 = preg_replace($search,$replace,$string);

echo "Result is:<BR>" . $string2;
echo "<BR><BR>Result should be:<BR> Say a [betterWord0] about
[betterWord1] and [betterWord2]";

// result should be:
//Say a [betterWord0] about [betterWord1] and [betterWord2]
//
// but produces: 
//Say a [betterWord0] about [betterWord2] and [betterWord1]
//
// Seems like the order in which i build my array
// is the order for replacing...and not the index
// of $array[$index]
//
// if you add a ksort it works fine:

ksort($search,SORT_NUMERIC);
ksort($replace,SORT_NUMERIC);		
reset($search);
reset($replace);		

$string3 = preg_replace($search,$replace,$string);
echo "<BR><BR>Corrected (ksort) Result is:<BR>" . $string3;

//
// Has someone an idea, about what's happening here?
//
// just a guy addicted to php ;-)
?>
 [2003-01-22 12:14 UTC] michael dot mauch at gmx dot de
The search strings used in the preg_replace() function are regular expressions. "[abc]" is a regular expression which matches any of the characters "a", "b" or "c".

Either use str_replace() to replace your strings, or learn to use regular expressions <http://de3.php.net/manual/en/pcre.pattern.syntax.php>.
 [2003-01-22 14:52 UTC] iliaa@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

The replacment is done in the order the array was created, if you want it to be done based on array keys that run an appropriate sorting function before executing the preg_replace function.
 [2003-01-22 15:30 UTC] pollita@php.net
This could be made more clear in the manual.  I'll add a note and an example tonight.
 [2003-01-22 19:09 UTC] pollita@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 19:01:32 2024 UTC