Bug #66507 [Nab]: string replace does not work on certain points on the string
| From: | hugo at domibay dot es | Date: | Fri, 17 Jan 2014 10:31:54 +0000 |
| Subject: | Bug #66507 [Nab]: string replace does not work on certain points on the string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-183866@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66507&edit=1
ID: 66507
User updated by: hugo at domibay dot es
Reported by: hugo at domibay dot es
Summary: string replace does not work on certain points on
the string
Status: Not a bug
Type: Bug
Package: mbstring related
Operating System: Centos 6.4
PHP Version: 5.4.24
Block user comment: N
Private report: N
New Comment:
I might have been a bit too quick about the Configuration.
Although the other one worked for me
this one actually might be more correct:
[mbstring]
mbstring.language = neutral
mbstring.internal_encoding = UTF-8
;mbstring.http_input = auto
mbstring.http_output = auto
Previous Comments:
------------------------------------------------------------------------
[2014-01-17 10:18:31] requinix@php.net
Good to hear it's fixed.
------------------------------------------------------------------------
[2014-01-17 10:17:20] hugo at domibay dot es
The Solution for this unexpected Output is to check on the Configuration at the "mbstring"
Section within the php.ini .
Change Default Configuration:
[mbstring]
;mbstring.language = Japanese
;mbstring.internal_encoding = EUC-JP
;mbstring.http_input = auto
;mbstring.http_output = SJIS
To this Configuration that works:
[mbstring]
mbstring.language = UTF-8
mbstring.internal_encoding = UTF-8
;mbstring.http_input = auto
mbstring.http_output = UTF-8
------------------------------------------------------------------------
[2014-01-17 10:12:24] hugo at domibay dot es
I was able to reproduce this:
$string = "ä½äº't Goor Park";
// ä½äº't Goor Park
echo $string, "\n";
// expected: ä½äº[apostrophe]t Goor Park
echo "encoding: '" . mb_regex_encoding() . "'\n";
echo mb_ereg_replace("[\']", "[apostrophe]", $string), "\n";
// expected ä½äº\'t Goor Park
mb_regex_encoding("UTF-8"); // previous value was EUC-JP
echo "encoding: '" . mb_regex_encoding() . "'\n";
echo mb_ereg_replace("[\']", "\\'", $string), "\n";
Produced the Output:
ä½äº't Goor Park
encoding: 'EUC-JP'
ä½äº't Goor Park
encoding: 'UTF-8'
ä½äº\'t Goor Park
So I went to my php.ini and changed the "mbstring" Section Options to:
mbstring.language = UTF-8
mbstring.internal_encoding = UTF-8
mbstring.http_output = UTF-8
Repeating then the Script gave me this new Output.
ä½äº't Goor Park
encoding: 'UTF-8'
ä½äº[apostrophe]t Goor Park
encoding: 'UTF-8'
ä½äº\'t Goor Park
Thank you for your help.
I couldn't find help on this and I was always thinking that I would have "UTF-8" as
internal encoding which I actually didn't have.
------------------------------------------------------------------------
[2014-01-17 09:37:47] requinix@php.net
Notice how mb_ereg_replace() never gave you the chance to tell it what encoding the string was in?
That would be a job for mb_regex_encoding().
<?php // I ran this from a file encoded in UTF-8
$string = "ä½äº't Goor Park";
// ä½äº't Goor Park
echo $string, "\n";
// ä½äº't Goor Park
echo mb_ereg_replace("[\']", "[apostrophe]", $string), "\n";
// ä½äº[apostrophe]t Goor Park
mb_regex_encoding("UTF-8"); // previous value was EUC-JP
echo mb_ereg_replace("[\']", "[apostrophe]", $string), "\n";
?>
------------------------------------------------------------------------
[2014-01-17 08:59:26] hugo at domibay dot es
Description:
------------
I want to introduce certain chinese text into a database. The SQL Query requires that all
apostrophes are escaped.
Like "'" -> "\\'" .
As I want to process chinese and other languages that don't use latin characters I opted for a
string replace with "mb_ereg_replace()".
I found that "mb_ereg_replace()" could replace latin character names within the Text and
also apostrophes between latin character passages but was unable to do that when a chinese character
was just before the apostrophes.
Like "ä½äº't Goor Park" -> "ä½äº\\'t Goor Park"
I tried to use this command to achieve this. On many texts it worked, but on this text it failed.
$srs = mb_ereg_replace("[\']", "\\'", $srs);
Curious was that I can detect the apostrophe with "mb_strpos()", but I can't replace
it.
$iapops = mb_strpos($srs, "'");
Test script:
---------------
This Script shows that some passages can be replaces but that important apostrophe can't be
touched, but yes it can be detected.
$iapops = mb_strpos($srs, "'");
if($iapops !== false)
{
echo "apostrophe found on '$iapops'\n";
echo "test 0: '$srs'\n";
$srs = mb_ereg_replace("(goor)", "[GREEN]", $srs, "i");
echo "test 1: '$srs'\n";
$srs = mb_ereg_replace("[\']", "[apostrophe]", $srs);
echo "test 2: '$srs'\n";
if(mb_strpos($srs, "'") !== false)
echo "replace failed!";
} //if($iapops !== false)
Expected result:
----------------
<p><b>é
åºä½ç½®</b> <br />Hotel - Restaurant Het Ros van
Twenteä½äºde Lutteï¼ä½äº[apostrophe]t [GREEN] ParkåHuize Keizer
Museuméè¿ã 该 4 æçº§é
åºä½äºSandstone Museum of Bad
Bentheimåæ¬ç¹æµ·å§åå ¡å°åºã</p><p><b>客æ¿</b>
<br />
é
åºæ 30
é´å®¢æ¿ï¼æä¾å¹³æ¿çµè§ã客æ¿è®¾æç§äººé³å°ãææä¾ç嫿çµè§å¯æ»¡è¶³æ¨ç娱ä¹éæ±ã便å©è®¾æ½å
æ¬ç´æ¨çµè¯ï¼ä»¥åä¿é©>
ç®±å书æ¡ã</p><p><b>ä¼é²ãSPAãé«ç«¯æå¡è®¾æ½</b>
<br
/>äº«åæ¡æ¿ç度å设æ½ï¼æè
å°è±å欣èµç¾æ¯ã</p><p><b>é¤é¥®</b>
<br
/>æ¨å¯ä»¥å°é¤å
享ç¨ä¸é¡¿ç¾é¤ï¼ä¹å¯ä»¥éæ©é
åºçéæ¶å®¢æ¿æå¡ã欢è¿å
临é
å§/é
å»ï¼ç¹ä¸æ¯å欢ç饮åï¼ç
饮ä¸çªã</p><p><b>å
å¡åå
¶ä»æå¡è®¾æ½</b> <br
/>ç¹è²æå¡/设æ½å
æ¬ä¼è®²å¤ç§è¯è¨çæå¡åãå
Œ
±åºå空è°åå¾ä¹¦é¦ãè¿å®¶é
åºçæ´»å¨è®¾æ½å
æ¬ä¼è®®å®¤>
ãå°ä¼è®®å®¤åå®´ä¼è®¾æ½ãé
åºæä¾å
è´¹å车设æ½ã</p>
Actual result:
--------------
<p><b>é
åºä½ç½®</b> <br />Hotel - Restaurant Het Ros van
Twenteä½äºde Lutteï¼ä½äº't [GREEN] ParkåHuize Keizer
Museuméè¿ã 该 4 æçº§é
åºä½äºSandstone Museum of Bad
Bentheimåæ¬ç¹æµ·å§åå ¡å°åºã</p><p><b>客æ¿</b>
<br />
é
åºæ 30
é´å®¢æ¿ï¼æä¾å¹³æ¿çµè§ã客æ¿è®¾æç§äººé³å°ãææä¾ç嫿çµè§å¯æ»¡è¶³æ¨ç娱ä¹éæ±ã便å©è®¾æ½å
æ¬ç´æ¨çµè¯ï¼ä»¥åä¿é©
ç®±å书æ¡ã</p><p><b>ä¼é²ãSPAãé«ç«¯æå¡è®¾æ½</b>
<br
/>äº«åæ¡æ¿ç度å设æ½ï¼æè
å°è±å欣èµç¾æ¯ã</p><p><b>é¤é¥®</b>
<br
/>æ¨å¯ä»¥å°é¤å
享ç¨ä¸é¡¿ç¾é¤ï¼ä¹å¯ä»¥éæ©é
åºçéæ¶å®¢æ¿æå¡ã欢è¿å
临é
å§/é
å»ï¼ç¹ä¸æ¯å欢ç饮åï¼ç
饮ä¸çªã</p><p><b>å
å¡åå
¶ä»æå¡è®¾æ½</b> <br
/>ç¹è²æå¡/设æ½å
æ¬ä¼è®²å¤ç§è¯è¨çæå¡åãå
Œ
±åºå空è°åå¾ä¹¦é¦ãè¿å®¶é
åºçæ´»å¨è®¾æ½å
æ¬ä¼è®®å®¤>
ãå°ä¼è®®å®¤åå®´ä¼è®¾æ½ãé
åºæä¾å
è´¹å车设æ½ã</p>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66507&edit=1