Jump to content

Saving file contents to new file (UTF-8)


random1

Recommended Posts

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

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.