Forum

Thread tagged as: Question, Problem

mysqli_set_charset($c, "utf8");

Hi, I'm using $DB = PerchDB::fetch(); but need to use: mysqli_set_charset($c, "utf8"); top stop £ sign coming out as black dimond with a question mark. �

what is the correct way to do this.

thanks in advance

tony

Tony Monckton

Tony Monckton 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Unless you define PERCH_DB_CHARSET to use something other than UTF8, Perch uses UTF8 by default.

If you're using UTF8 and serving your pages as UTF8 you should have no issues.

this is being displayed in perch shop/orders/order details. and the database is set as utf8 - default collation

I've found the only way to get £ to display correctly is to use. mysqli_set_charset($c, "utf8"); I've had this issue with other sites, but this time its inside perch.

how can I get the DB link, or where in perch is fetch() function.

thanks in advance

tony

Drew McLellan

Drew McLellan 2638 points
Perch Support

What happens if you enter a £ in a regular Perch content region?

£ in regular perch content, appears on website ok.

so not sure what up...

had a look at perch/core/perchDB. but looks tricky to change.

tony

this works!!!

function to_utf8( $string ) {
// From https://w3.org/International/questions/qa-forms-utf-8.html
    if ( preg_match('%^(?:
      [\x09\x0A\x0D\x20-\x7E]            # ASCII
    | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
    | \xE0[\xA0-\xBF][\x80-\xBF]         # excluding overlongs
    | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
    | \xED[\x80-\x9F][\x80-\xBF]         # excluding surrogates
    | \xF0[\x90-\xBF][\x80-\xBF]{2}      # planes 1-3
    | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
    | \xF4[\x80-\x8F][\x80-\xBF]{2}      # plane 16
)*$%xs', $string) ) {
        return $string;
    } else {
        return iconv( 'CP1252', 'UTF-8', $string);
    }
}

$shippingMethod = @urldecode($shopLogArray['custom']);
if ( empty($shippingMethod) )   { $shippingMethod = "error";    }
    else {  $shippingMethod = to_utf8($shippingMethod);   }
Drew McLellan

Drew McLellan 2638 points
Perch Support

That you need this is an indication of a bigger problem in your code. Perch handles UTF8 without issue, so it must be something in your code that is introducing this problem.

my code i added to: orders.detail.pre.php

   $DB              = PerchDB::fetch();

  $orderTxnID = $Order->orderTxnID();

   $sql                 = "SELECT logData FROM perch2_shop_log WHERE logTransaction = '".$orderTxnID."'"; 
   $gShopLog    = $DB->get_rows($sql);

$a = explode('&',$gShopLog[0]["logData"]);
if (!empty($a))
{
    foreach ($a as $result) {
        $b = explode('=', $result);
                $shopLogArray[$b[0]] = $b[1];
    }
}

$shippingMethod = @urldecode($shopLogArray['custom']);

I need to extract the paypal custom ( for shipping method) and option_name & option_selection. so my client can see the size colour and shipping in order details page.

the logData is coming from paypal.

tony

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok - is it working, or do you have a question about it?

Hi Drew,

the char encoding for logData is ASCII, so not sure why £ nor display right. but I have to use the to_utf8 function.

thanks for your help.

tony