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
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.

Link to comment
Share on other sites

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

Edited by kenw232
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.