mbfpdf does not work - php

I try to make a pdf file which has some japanese character. However, the output file is some strange character. I use mbfpdf instead of fpdf.
<?php
define('FPDF_FONTPATH','fpdf/font/');
require('fpdf/mbfpdf.php');
$pdf=& new MBFPDF('P','mm','A4');
$pdf->AddMBFont(GOTHIC ,'EUC-JP');
$pdf->AddPage();
$pdf->SetFont(GOTHIC,'',20);
$pdf->Write(20,'日本語');
$pdf->Output('test.pdf');
?>

You can convert to ISO-8859-1 with utf8_decode() (some inaccuracy):
$str = utf8_decode($str);
or if iconv extension is available (preferred):
$str = iconv('UTF-8', 'windows-1252', $str);

Add the below line inside the head tags
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
If you are getting garbage texts after executing mysql queries, execute both the below queries first.
SET NAMES utf8
SET CHARACTER SET utf8

Related

php convert .html file charset from windows-1256 to utf-8

There are about 100,000 html files that are in windows-1256 charset and I need to convert them to utf-8 using file functions of php.
this is my code to convert one of them:
<?php
$content = file_get_contents('files/01.htm');
$content = iconv('windows-1256', 'utf-8', $content);
file_put_contents('files/01.htm', $content);
?>
after executing this code and visiting the 01.htm the charachters were unformatted so I edited the 01.htm and replaced
<META content="text/html ;charset=windows-1256" http-equiv=content-Type >
with
<META content="text/html ;charset=utf-8" http-equiv=content-Type>
But the characters are not utf-8, they are still unformatted. what is wrong with my code?

Special Characters In Php and MySQL

I've got a problem with some specials characters in PHP. I have a table in mysql (utf8_hungarian_ci) that contains some text with special characters like á, á, Ó, Ö, ö, ü, and I would like to show this text on my page. I've tested:
$text = htmlentities($text); //to convert the simple spec chars
$search = array("& otilde;","&O tilde;","& ucirc;","&U circ;");
$replace = array("& #337;","& #336;","& #369;","& #368;");
$text = str_replace($search, $replace, $text);
echo $text;
But this code works only if $text isn't set from database. If I use this code and my $text is selected from database, it doesn't shows me any text, and if I only use:
echo $text; without htmlentities and replacements
I get characters like this one: �
I know there were some questions about this and I have tried accepted answers, but it still doesn't work, so please help me if you want and if you have time. Thank you anyway. A good day to you all!
Also try setting in your header to use UTF-8 encoding.
In your PHP file, add
header('Content-type: text/html; charset=utf-8');
as well as specifying the encoding to be UTF-8 in your <meta> tag, to ensure that you told the browser. And see if it fixes the issue.
As well as including UTF-8 encoding in your meta tag.
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
...
</head>
Edit:
If you have access to Apache configuration, see if AddDefaultCharset is set to another encoding.
Try using mysql_set_charset() (mysqli_set_charset() if you're using MySQLi).
Try to put this in you html header:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
(Also, you may need to save your file in "utf-8" file encoding)
.
Secondly, you could use this to try to tranlate-or-remove the disturbing char that always prints out in your case:
$str_out = #iconv("ISO-8859-1", "UTF-8//TRANSLIT//IGNORE", $str_in);
This is a slightly generic answer but please read up this article I wrote on common character-encoding pitfalls in the PHP/MySQL stack and if you still have problems let's try to work through them.
http://webmonkeyuk.wordpress.com/2011/04/23/how-to-avoid-character-encoding-problems-in-php/

encoding issue using php and mysql, wordpress

I have the following text which I manually enter into the wordpress posts table
&#226;€š
I encode into utf-8 using:
$text = "&#226;€š";
$enc = mb_detect_encoding($text, "UTF-8,ISO-8859-1");
$hotelDescription = iconv($enc, "UTF-8", $text);
However, when wordpress echoes it it displays
‚
Any ideas who I can output the correct characters?
You need to specify that the page displaying that string render using UTF-8 encoding. The output you posted is the iso-8859-1 version of that utf-8 string. Assuming the data is being stored in the database correctly as UTF-8 ensure the page where this string is being rendered has the following meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
Looks like you're missing the & before #226;

PHP decode GB2312

I'm working on an IMAP email script and I have some lines coded in GB2312 (which I assume is Chinese encoding), looks like this =?GB2312?B?foobarbazetc
How can I start working with this string? I checked mb_list_encodings() and this one is not listed.
If you have the base64-decoded data, then use mbstring or iconv. If you have the raw header, then mbstring.
<?php
$t = "\xc4\xe3\xba\xc3\n";
echo iconv('GB2312', 'UTF-8', $t);
echo mb_convert_encoding($t, 'UTF-8', 'GB2312');
mb_internal_encoding('UTF-8');
echo mb_decode_mimeheader("=?gb2312?b?xOO6ww==?=");
?>
Ignacio solved the meat of the problem with mb_decode_mimeheader() but for future reference these links are also helpful:
http://developer.loftdigital.com/blog/php-utf-8-cheatsheet
http://www.herongyang.com/PHP-Chinese/PHP-UTF-8-Chinese-String-Literals.html
The specific header string I was working with:
$subject = "=?GB2312?B?tPC4tDogUXVvdGF0aW9uIFBJSSBwcm9kdWN0cyA=?= =?GB2312?B?Rk9CIFNoYW5naGFpIG9yIE5pbmdibyBwb3J0?="
This required a page header of
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
and PHP
mb_internal_encoding('utf-8');
echo mb_decode_mimeheader($subject)."<br />";
to output
主题: Quotation PII products FOB Shanghai or Ningbo port

special characters to html entity using php

I want to convert special characters like ñ, Ñ to htmlentities using php.
I tried using htmlentities, but instead of returning "&ntilde" for its value it returns "ñ" as its value.
Make sure that your page charset is set to utf-8
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
You need to specify the character set you use as the third parameter to htmlentities(). The default character set is iso-8859-1. If you use UTF-8 for your data, you need to say so:
$result = htmlentities($string, ENT_QUOTES, "UTF-8");
You have to specify the charset because the default is ASCII (http://php.net/manual/en/function.htmlentities.php):
htmlentities($stringToConvert, ENT_COMPAT, 'UTF-8')

Categories