Jump to content

Problem with str_replace on UTF8 string


kenw232

Recommended Posts

I have a string that gets posted to a page.  Its a UTF8 string I guess because this outputs "UTF8":

 

<HTML>

   <? echo mb_detect_encoding ($_REQUEST["variable"]); ?>

</HTML>

 

When I try to replace any special MS Word characters it simply never works.

 

<?

   echo str_replace("–", "-", $_REQUEST["varaible"]);

?>

 

Where the dash is that extended MSword dash and I want to replace it with a normal dash.  Anyone know why?  I'm using PHP Version 5.3.16 on Linux.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/278572-problem-with-str_replace-on-utf8-string/
Share on other sites

You'd need to ensure your PHP file is also saved in UTF-8 for something like that to work. str_replace is not character-set aware, it just does a binary replace so the character sets of all the arguments would have to match for it to work properly.

I have <meta http-equiv="Content-type" content="text/html; charset=utf-8"/> at the top of my html page that does the str_replace.  And I can save in UTF8 format right into a mysql database if I try.  I just see no way to get str_replace, or anything else to replace special word characters first.  They are always ignored.

 

This works:   echo str_replace("T", "replace", $_REQUEST["variable"]); // Normal ASCII T character
This does not:   echo str_replace("…", "replace", $_REQUEST["variable"]);  // where ... is that special word character

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.