kenw232 Posted May 29, 2013 Share Posted May 29, 2013 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. Quote Link to comment Share on other sites More sharing options...
kicken Posted May 29, 2013 Share Posted May 29, 2013 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 29, 2013 Share Posted May 29, 2013 And the webpage has to be UTF-8 too (if that's where this string is coming from) otherwise that "variable" thing might be an HTML entity-encoded string. Quote Link to comment Share on other sites More sharing options...
kenw232 Posted May 29, 2013 Author Share Posted May 29, 2013 (edited) 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 characterThis does not: echo str_replace("…", "replace", $_REQUEST["variable"]); // where ... is that special word character Edited May 29, 2013 by kenw232 Quote Link to comment Share on other sites More sharing options...
kenw232 Posted May 29, 2013 Author Share Posted May 29, 2013 Ok, I got it. Even the files had to be UTF8. I didn't know plain text files were this specific now. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 30, 2013 Share Posted May 30, 2013 Files are nothing but big strings stored on a hard drive, right? So text encodings apply to files too. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.