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. Link to comment https://forums.phpfreaks.com/topic/278572-problem-with-str_replace-on-utf8-string/ 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. Link to comment https://forums.phpfreaks.com/topic/278572-problem-with-str_replace-on-utf8-string/#findComment-1433075 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. Link to comment https://forums.phpfreaks.com/topic/278572-problem-with-str_replace-on-utf8-string/#findComment-1433078 Share on other sites More sharing options...
kenw232 Posted May 29, 2013 Author Share Posted May 29, 2013 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 Link to comment https://forums.phpfreaks.com/topic/278572-problem-with-str_replace-on-utf8-string/#findComment-1433080 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. Link to comment https://forums.phpfreaks.com/topic/278572-problem-with-str_replace-on-utf8-string/#findComment-1433086 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. Link to comment https://forums.phpfreaks.com/topic/278572-problem-with-str_replace-on-utf8-string/#findComment-1433095 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.