ID:               21816
 User updated by:  [EMAIL PROTECTED]
 Reported By:      [EMAIL PROTECTED]
 Status:           Open
 Bug Type:         *Regular Expressions
 Operating System: Windows XP
 PHP Version:      4.3.0
 New Comment:

<?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 ;-)
?>


Previous Comments:
------------------------------------------------------------------------

[2003-01-22 07:43:39] [EMAIL PROTECTED]

<?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 ;-)
?>

------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=21816&edit=1

Reply via email to