#40871 [Opn->Bgs]: preg_replace returns blank when the text contains bad UTF-8

From: Date: Tue, 20 Mar 2007 19:58:25 +0000
Subject: #40871 [Opn->Bgs]: preg_replace returns blank when the text contains bad UTF-8
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-110605@lists.php.net to get a copy of this message
ID: 40871 Updated by: tony2001@php.net Reported By: ismith at motorola dot com -Status: Open +Status: Bogus Bug Type: PCRE related Operating System: Windows Server 2003 SP1 PHP Version: 5.2.1 New Comment: This is what the underlying PCRE library returns. Previous Comments: ------------------------------------------------------------------------ [2007-03-20 19:54:33] ismith at motorola dot com 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 &#915;Ç£ and COWS"; now "I hate WOMBATS &#915;Ç£ and COWS" Was "I love BEARS &#915;Ç? and LIONS"; now "I love BEARS &#915;Ç? and LIONS" Actual result: -------------- Was "I hate WOMBATS &#915;Ç£ and COWS"; now "I hate WOMBATS &#915;Ç£ and COWS" Was "I love BEARS &#915;Ç? and LIONS"; now "" ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=40871&edit=1

« previous php.bugs (#110605) next »