1

I'm generating a QR code vCard using PHP QR Code (http://phpqrcode.sourceforge.net). It works, but not with Brazillian Portuguese characters. I can't find how to force UTF-8 to it.

The problem is that iOS will not recognize the characters, check the screenshots below (iOS and Android).

<?php 

include('phpqrcode/qrlib.php'); 

// how to build raw content - QRCode with detailed Business Card (VCard) 
$tempDir = ""; 

// here our data 
$name         = 'João Carlos da Silva'; 
$sortName     = 'da Silva;João Carlos'; 
$phone        = '+55 (89) 2345-6789'; 
$phonePrivate = '+55 (94) 4521-3989'; 
$phoneCell    = '+55 (66) 1234-5678'; 
$orgName      = 'GH Construtora'; 

$email        = 'emaildousuario@dominio.com.br'; 

// if not used - leave blank! 
$addressLabel     = 'Escritório'; 
$addressPobox     = ''; 
$addressExt       = '2º andar'; 
$addressStreet    = 'Av. das Nações, 200'; 
$addressTown      = 'Cidade'; 
$addressRegion    = 'SP';
$addressPostCode  = '18.902-100'; 
$addressCountry   = 'Brasil';

// we building raw data 
$codeContents  = 'BEGIN:VCARD'."\n"; 
$codeContents .= 'VERSION:2.1'."\n"; 
$codeContents .= 'N:'.$sortName."\n"; 
$codeContents .= 'FN:'.$name."\n"; 
$codeContents .= 'ORG:'.$orgName."\n"; 

$codeContents .= 'TEL;WORK;VOICE:'.$phone."\n"; 
$codeContents .= 'TEL;HOME;VOICE:'.$phonePrivate."\n"; 
$codeContents .= 'TEL;TYPE=cell:'.$phoneCell."\n"; 

$codeContents .= 'ADR;TYPE=work;'. 
    'LABEL="'.$addressLabel.'":' 
    .$addressPobox.';' 
    .$addressExt.';' 
    .$addressStreet.';' 
    .$addressTown.';' 
    .$addressPostCode.';' 
    .$addressCountry 
."\n"; 

$codeContents .= 'EMAIL:'.$email."\n"; 

$codeContents .= 'END:VCARD'; 

// generating 
QRcode::png($codeContents, $tempDir.'026.png', QR_ECLEVEL_L, 7); 

// displaying 
echo '<img src="026.png" />';

Android iOS

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Go through https://stackoverflow.com/questions/279170/utf-8-all-the-way-through?rq=1 in its entirety. There are too many possible reasons and you'll probably find everything in there to fix this. – Funk Forty Niner Nov 07 '18 at 23:47
  • It doesn't help me (I checked). My page is set to UTF-8, and my strings are UTF-8, my issue is the return of the QR Code, which either is not returning UTF-8 or I need to use some other encoding so iOS reads it right. Please consider re-opening my question. – Guilherme Corrêa Peralta Nov 08 '18 at 00:28
  • I doubt the QR code is doing this. UTF-8 has 2 different methods of saving. One "with" the BOM (byte order mark) and one without. "All" files "must" be saved in the same format. If one fails, the rest fails. You could also consider saving as ANSI; sometimes that works and I had to use that for something some time ago myself. This is definitely an encoding issue. – Funk Forty Niner Nov 08 '18 at 01:26
  • I fixed the code now. Tried everything related to the encoding issue, but nothing changed, everything is UTF-8 already. The real problem was that the example code I used to generate the QR Code was setting the VCARD version to 2.1. When I realized that, I changed to 3.0 and it works perfectly now with iOS and Android. It was not an encoding issue after all. Thank you. – Guilherme Corrêa Peralta Nov 08 '18 at 03:45
  • That's great news. I reopened the question so you can post your own answer now. I rolled the question back. Edit: I also added the "vcard" tag which is relevant to the question/problem. – Funk Forty Niner Nov 08 '18 at 11:13

2 Answers2

3

Solution: QR Code was setting the VCARD version to 2.1. I changed to 3.0 and it works perfectly now with iOS and Android. It was not an encoding issue.

$codeContents .= 'VERSION:3.0'."\n";
0

I don't think the QR-Code is doing something wrong. The Smartphone App is reading, whatever the QR-Code contains.

The Android-App might apply a little more logic afterwards, including some encoding detection, but the QRs content is the same.

So, try to encode your data BEFORE generating the QR-Code:

$codeContents .= 'FN:'.utf8_encode($name)."\n"; 
dognose
  • 20,360
  • 9
  • 61
  • 107