- Convert files encoding
- 4 Answers 4
- Перекодировка текста UTF-8 и WINDOWS-1251
- windows-1251 в UTF-8
- UTF-8 в windows-1251
- Когда ни что не помогает
- File_get_contents / CURL
- How to encode a PHP file with base64
- 2 Answers 2
- Related
- Hot Network Questions
- Subscribe to RSS
- Read ansi file and convert to UTF-8 string
- 3 Answers 3
- Update
Convert files encoding
I have a PHP application who’s files encoding is Greek ISO (iso-8859-7). I want to convert the files to utf-8 but simply saving the files with utf-8 isn’t enough since the Greek texts get garbled. Is there an «automatic» method to do this so that I can completely convert my app’s encoding without having to go through each file and rewrite the texts?
4 Answers 4
On a Linux system, if you are sure all files are currently encoded in ISO-8859-7, you can do this:
bash> find /your/path -name "*.php" -type f \ -exec iconv "<>" -f ISO88597 -t UTF8 -o "<>.tmp" \; \ -exec mv "<>.tmp" "<>" \;
This converts all PHP script files located in /your/path as well as all sub-directories. Remove -name «*.php» to convert all files.
Since you are under Windows, the easiest option would be a PHP script like this:
$file)< if($file->isFile()) file_put_contents( $fileName, iconv('ISO-8859-7', 'UTF-8', file_get_contents($fileName)) ); >
$new_string = iconv("ISO-8859-7", "UTF-8", $old_string);
This will only convert the contents, I would like to entirely convert the files, including the contents.
Ah, I read your last sentence as how to automatically convert the data without having to manually retype it. You are going to have to write your own function to transverse your app and update the encoding of your files. If iconv doesn’t work for you, try mb_convert_encoding (php.net/manual/en/function.mb-convert-encoding.php). Also when you say the texts gets garbled, is that when viewing the file in a text editor?, or when you output contents of the file within PHP?
Did you send a UTF8 content type header with the output? As well as set the content type to utf8 in the html?
Yes. The problem resides in the fact that the original app encoding was iso-8859-7, not only the data from the db but the files as well.
Перекодировка текста UTF-8 и WINDOWS-1251
Проблема кодировок часто возникает при написании парсеров, чтении данных из xml и CSV файлов. Ниже представлены способы эту проблему решить.
windows-1251 в UTF-8
$text = iconv('windows-1251//IGNORE', 'UTF-8//IGNORE', $text); echo $text;
$text = mb_convert_encoding($text, 'UTF-8', 'windows-1251'); echo $text;
UTF-8 в windows-1251
$text = iconv('utf-8//IGNORE', 'windows-1251//IGNORE', $text); echo $text;
$text = mb_convert_encoding($text, 'windows-1251', 'utf-8'); echo $text;
Когда ни что не помогает
$text = iconv('utf-8//IGNORE', 'cp1252//IGNORE', $text); $text = iconv('cp1251//IGNORE', 'utf-8//IGNORE', $text); echo $text;
Иногда доходит до бреда, но работает:
$text = iconv('utf-8//IGNORE', 'windows-1251//IGNORE', $text); $text = iconv('windows-1251//IGNORE', 'utf-8//IGNORE', $text); echo $text;
File_get_contents / CURL
Бывают случаи когда file_get_contents() или CURL возвращают иероглифы (ÐлмазнÑе боÑÑ) – причина тут не в кодировке, а в отсутствии BOM-метки.
$text = file_get_contents('https://example.com'); $text = "\xEF\xBB\xBF" . $text; echo $text;
Ещё бывают случаи, когда file_get_contents() возвращает текст в виде:
Это сжатый текст в GZIP, т.к. функция не отправляет правильные заголовки. Решение проблемы через CURL:
function getcontents($url) < $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $output = curl_exec($ch); curl_close($ch); return $output; >echo getcontents('https://example.com');
How to encode a PHP file with base64
🙂 I have one ridiculously silly question and most of you would like to reffer me to Google right away, but that didn’t helped me out within the first hour. I suppose I didn’t knew how to look for. I’m having a PHP file and I’d like to have it in base64 yet I can’t get it to work anyhow. 1) I encoded my PHP script to base64(and included the PHP tags). It’ll look as following : JTNDJTNGcGhwJTIwVGhpcyUyMGlzJTIwdGhlJTIwUEhQJTIwY29kZSUyMCUzRiUzRQ== This kind of base64 won’t execute so I added the PHP tags to it although the encoded file already had it. Still didn’t worked out. Removed the tags from the base64 and tried again, but still didn’t worked. Then I tried adding the PHP tags and inside of them added : eval(gzinflate(base64_decode(‘base64 here’))); Still didn’t worked out anyhow. Is anyone here kind enough to tell the kiddo how to run a base64 encoded PHP file properly? Would be really appreaciated. 🙂
2 Answers 2
$source = "JTNDJTNGcGhwJTIwVGhpcyUyMGlzJTIwdGhlJTIwUEhQJTIwY29kZSUyMCUzRiUzRQ= JTNDJTNGcGhwJTIwVGhpcyUyMGlzJTIwdGhlJTIwUEhQJTIwY29kZSUyMCUzRiUzRQ= http://eaccelerator.net/" rel="noreferrer">this one or this one. They will crypt your codes and make them even faster!