Viewed   131 times

I have a table where the data are stored like boîtes with none utf8 characters. Now I have my php script which works fine on my local machine.

$utf = utf8_decode($details);
echo "UTF8-DE : ".$utf."<hr>";-> `boîtes`

When I put this script on another machine its not working its echoing boîtes . I am sure it depends on the charset of the php or server? Any help please

 Answers

1

Try set_charset function

For mySqli

mysqli_set_charset($connection, "utf8");

For mySql

mysql_set_charset("UTF8", $connection);

Alternative

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
Friday, September 23, 2022
5

Apache

The server encoding must be either not set, or set to UTF-8. This is done via the apache AddDefaultCharset directive. This can go to the virtualhost or the general file (see documentation).

AddDefaultCharset utf-8

MySql

  • Set the collation of the database to be UTF-8
  • Set the connection encoding. It can be done as someone said with mysqli_set_charset, or by sending this just after connecting:
    SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'

PHP

1- You should set the HTML charset of the page to be UTF-8, via a meta tag on the page, or via a PHP header:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-or-
    header('Content-type: text/html; charset=utf-8');

2- You should always use the mb* version of string-related functions, for example, mbstrlen instead of strlen to get the string length of a string.

This should allow you to have UTF-8 everywhere, from the pages to the data. A test you can do: right-click anywhere on the page using firefox, and select Show page information. The effective encoding is listed in that page.

Saturday, December 3, 2022
 
jlig
 
5

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:

<?php
$path = realpath('C:\your\path');

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path), 
    RecursiveIteratorIterator::SELF_FIRST
);

foreach($iterator as $fileName => $file){
    if($file->isFile())
        file_put_contents(
            $fileName,
            iconv('ISO-8859-7', 'UTF-8', file_get_contents($fileName))
        );
}
Sunday, September 18, 2022
 
3

I assume you're sending these queries from PHP. I can say that because you are me.

Apparently 'utf8_turkish_ci' collation can decode gibberish utf8 codes but not plain Turkish characters. Changing encodings on php files by header('Content-Type: text/html; charset=utf8'); you can overcome this problem.

Wednesday, December 14, 2022
 
5

Use cURL. This function is an alternative to file_get_contents.

function url_get_contents($Url) {
if (!function_exists('curl_init')){
    die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$data = url_get_contents("http://www.bratm.sk/trochu-harlem-inspiracie-zaslite-nam-svoj-super-harlem-shake-ak-bude-husty-zverejnime-ho/");
print_r($data);
Thursday, September 1, 2022
 
devsnd
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :