From: cold_candor at hotmail dot com
Operating system: windows XP Pro
PHP version: 5.0.5
PHP Bug Type: PCRE related
Bug description: repeated character in output of preg_replace
Description:
------------
I can't imagine any reason why this should happen, and all the
documentation I've read says it shouldn't, so here you go:
If, when using the preg_replace function, I attempt to match against an
entire line to remove leading and ending whitespace (using ^ and $ but no
modifiers, like D or m), the function does what I'd expect. However, if I
neglect to use the leading ^, the function still matches what I'd expect it
to, but the replacement string is entered twice! Even if I don't use the
backreference in the replacement, all non-reference related characters are
still repeated!
If I split the replacement to deal with leading and ending whitespace
seperately, it always repeats the replacement string!
I did not configure anything special when I downloaded PHP, I simply
grabbed the windows zip file provided on the website (www.php.net), opened
it, added the path to my environment variables, and started using it.
I know of nothing special about my setup.
I have made no changes to the PHP.ini file
If it turns out that this is not a bug, don't tell me to use the damn
support page, everything on there I either can't do or have tried with no
result or have no entries relating to my problem or (what I really want)
have no way to actually ask a question. Since you have to figure it out
anyway, just tell me what went wrong!
Reproduce code:
---------------
<?php
$text = " a b c ";
$newText = preg_replace("/\s*(.*?)\s*$/", "$1\n", $text);
for($i = 0; $i < strlen($newText); $i++) {
echo ord($newText[$i]), '~';
} // End for loop
echo "\n$newText";
$newText = preg_replace("/\s*(.*?)\s*$/", " $1\n gh ", $text);
for($i = 0; $i < strlen($newText); $i++) {
echo ord($newText[$i]), '~';
} // End for loop
echo "\n$newText";
$newText = preg_replace("/^\s*/", "", $text);
$newText = preg_replace("/\s*$/", "\n", $newText);
for($i = 0; $i < strlen($newText); $i++) {
echo ord($newText[$i]), '~';
} // End for loop
echo "\n$newText";
?>
Expected result:
----------------
The following three outputs should be produced (labels added for
readability):
ASCII values: 97~32~98~32~99~10~
Viewed output: "a b c
"
ASCII values: 32~32~97~32~98~32~99~10~32~103~104~32
Viewed output: " a b c
gh "
ASCII values: 97~32~98~32~99~10~
Viewed output: "a b c
"
Actual result:
--------------
The following three outputs are what was actually produced (labels added
for readability):
ASCII values: 97~32~98~32~99~10~10~
Viewed output: "a b c
"
ASCII values:
32~32~97~32~98~32~99~10~32~103~104~32~32~32~10~32~103~104~32~
Viewed output: " a b c
gh
gh"
ASCII values: 97~32~98~32~99~10~10~
Viewed output: "a b c
"
--
Edit bug report at http://bugs.php.net/?id=34947&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=34947&r=trysnapshot4
Try a CVS snapshot (php5.0):
http://bugs.php.net/fix.php?id=34947&r=trysnapshot50
Try a CVS snapshot (php5.1):
http://bugs.php.net/fix.php?id=34947&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=34947&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=34947&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=34947&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=34947&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=34947&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=34947&r=support
Expected behavior: http://bugs.php.net/fix.php?id=34947&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=34947&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=34947&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=34947&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=34947&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=34947&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=34947&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=34947&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=34947&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=34947&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=34947&r=mysqlcfg