#40871 [NEW]: preg_replace returns blank when the text contains bad UTF-8
| From: | ismith at motorola dot com | Date: | Tue, 20 Mar 2007 19:54:33 +0000 |
| Subject: | #40871 [NEW]: preg_replace returns blank when the text contains bad UTF-8 | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-110604@lists.php.net to get a copy of this message | ||
From: ismith at motorola dot com
Operating system: Windows Server 2003 SP1
PHP version: 5.2.1
PHP Bug Type: PCRE related
Bug description: preg_replace returns blank when the text contains bad UTF-8
Description:
------------
I am using preg_replace to do a search and replace on some text which
contains an invalid UTF-8 code sequence. I am using the "u" modifier.
I believe that preg_replace should suppress the bad character, or replace
it with an appropriate error marker; but otherwise return the text intact
(after making the required replacements).
Both preg_replace and preg_replace_callback return an empty string in this
case, even when the search pattern matches nothing in the input.
Reproduce code:
---------------
<?php
// Text with a valid UTF-8 character sequence.
$goodText = "I hate WOMBATS \342\200\234 and COWS";
// Text with an invalid UTF-8 character sequence.
$badText = "I love BEARS \342\200\077 and LIONS";
$good2 = preg_replace("/ELEPHANTS/iu", "MICE", $goodText);
printf("Was \"%s\"; now \"%s\"\n", $goodText, $good2);
$bad2 = preg_replace("/ELEPHANTS/iu", "MICE", $badText);
printf("Was \"%s\"; now \"%s\"\n", $badText, $bad2);
?>
Expected result:
----------------
Was "I hate WOMBATS ΓÇ£ and COWS"; now "I hate WOMBATS ΓÇ£ and
COWS"
Was "I love BEARS ΓÇ? and LIONS"; now "I love BEARS ΓÇ? and
LIONS"
Actual result:
--------------
Was "I hate WOMBATS ΓÇ£ and COWS"; now "I hate WOMBATS ΓÇ£ and
COWS"
Was "I love BEARS ΓÇ? and LIONS"; now ""
--
Edit bug report at http://bugs.php.net/?id=40871&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=40871&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=40871&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=40871&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=40871&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=40871&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=40871&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=40871&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=40871&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=40871&r=support
Expected behavior: http://bugs.php.net/fix.php?id=40871&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=40871&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=40871&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=40871&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=40871&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=40871&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=40871&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=40871&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=40871&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=40871&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=40871&r=mysqlcfg