random1 Posted November 24, 2008 Share Posted November 24, 2008 I am using the following code to resave a file to UTF-8 character encoding. Each time I open the save file it remains as ASCII. Any ideas? <?php function convertdata($source, $targetencoding) { // detect the character encoding of the incoming file $encoding = mb_detect_encoding($source, 'auto'); // escape all of the question marks so we can remove artifacts from // the unicode conversion process $target = str_replace("?", "[question_mark]", $source); // convert the string to the target encoding $target = mb_convert_encoding($target, $targetencoding, $encoding); // remove any question marks that have been introduced because of illegal characters $target = str_replace("?", "", $target); // replace the token string "[question_mark]" with the symbol "?" $target = str_replace( "[question_mark]", "?", $target ); return $target; } $file = 'classes/calendar.php'; $file2 = 'classes/calendartemp2.php'; $file_contents = file_get_contents($file); $fh = fopen($file, 'r'); $fh2 = fopen($file2, 'w'); // $file_contents = str_replace('foo','bar',$file_contents); $file_contents = convertdata($file_contents, 'UTF-8'); fwrite($fh2, $file_contents); fclose($fh2); fclose($fh); ?> Link to comment https://forums.phpfreaks.com/topic/133970-saving-file-contents-to-new-file-utf-8/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.