special characters to html entity using php - 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')

Related

Urldecode scandinavian ascii values

I need help with decoding url encoded scandinavian ASCII values with PHP.
I have tried decode å character like this:
$string = "%e5";
echo rawurldecode($string);
But this gives black diamond �. Same result with urldecode() function.
I am using <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8"> in head.
When using rawurldecode() function on English letters like %61 it works great.
See http://www.backbone.se/urlencodingUTF8.htm for all url encoded ASCII codes.
E5 is the ISO-8859-1 encoded representation of the character å.
Your problem is that you're outputting an ISO-8859-1 encoded string, yet are telling the browser to interpret it as UTF-8. Either change the encoding in your HTTP headers/meta tag, or convert the string from 8859 to UTF-8:
echo utf8_encode(rawurldecode('%e5'));
(There's almost never a good time for utf8_encode, but in this case it actually succinctly performs the needed charset conversion. Usually you should prefer explicit charset conversions using iconv or mb_convert_encoding.)
utf-8 character set does not contain e5 code.
Please check a table with utf-8 charset .
Try with a valid utf-8 string.
"scandinavian ascii" character set is not supported by rawurldecode .
Try one of the functions iconv,that support CP865 (I guess this is the character set for which you want support):
http://php.net/manual/ro/function.iconv-mime-decode.php
http://php.net/manual/ro/function.iconv-mime-decode-headers.php

php/mysql - foreign characters

I am having problems displaying foreign characters (characters with accents like: é à ù ç and so on)
The row in the database is like this:
Name | Datatype | Charset
title | varchar(255) | utf8_general_ci
I store it like this:
function inputFilter($var)
{
$var = trim(htmlentities(strip_tags($var)));
if (get_magic_quotes_gpc())
$var = stripslashes($var);
$var = mysql_real_escape_string($var);
return $var;
}
$title = inputFilter($_POST['title']);
and I print it like this:
print $getfromdb['title'];
This is how it's printed out:
Português //Should be: Português
I have tried adding:
htmlspecialchars, utf8_decode/encode and htmlentities to the print, although nothing helps!
I've added this to the header:
<meta charset="utf-8">
What am I doing wrong?
Steps to Follow:
Use the meta tag for UTF8.
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Set your PHP to use UTF8.
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
For mysql, you want to convert your table to UTF8.
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
Also you can run:
SET NAMES UTF8
as the first query after establishing a connection which will convert your DB connection to UTF8.
Include mysqli_set_charset($link, "utf8"); right after every connection you make. This will work.
Try this:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8"> is HTML 5 (and, BTW, UTF-8 is uppercase)
Since it looks like your test chain also involves a form (because of the $_POST), you must make sure that the UTF-8 charset is set for the form too.
Use SET NAMES utf8 before you query/insert into the database
query("SET NAMES utf8");
You should use character encoding as ISO-8859-1 instead of UTF-8 as follows:
<meta charset="ISO-8859-1">
The characters you are trying to show are latin and UTF-8 i.e. UNICODE encoding cannot interpret latin characters.
Reference
And in case of mysql you should use latin1 charset.

UTF-8 and ÅÄÖ special characters PHP

Hi have a look at this picture: http://ctrlv.in/175196 and as you can see the ÅÄÖ are replaced with �.
I have this at the very top of my php: <meta http-equiv="content-type" content="text/html; charset=utf-8"></meta>
and when I look at source it is indeed utf-8 - so why dont they display properly?
When you see the UNICODE REPLACEMENT CHARACTER �, it means your text is being interpreted as UTF-8 (or another Unicode encoding), but one of the byte sequences in the file was not valid in this encoding.
In other words, the file is not UTF-8 encoded.
try this.
iconv('windows-1250', 'utf-8', $your_variable);
if that is coming from an sql query set_charset('utf8') first before the query.
using notepad++ try encoding utf-8 without bom

mbfpdf does not work

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

How can i decode my html entities chars (ÆØÅ)?

$test = "selvfølgelig";
$test = html_entity_decode($test);
I want this to output "selvfølgelig", but this turns out as selvf�lgelig
How can i make this output like i want?
header("Content-Type: text/html; charset=UTF-8");
$string="selvfølgelig";
echo html_entity_decode($string, ENT_QUOTES, 'UTF-8')
i think you dont need to actually use this function.
EDIT: you can use the
< meta >
tag that Qualcuno provided instead of throwing a header.
It's an encoding issue, not related to html_entity_decode.
Make sure your page is in UTF-8: put this tag in your <head></head> section of the page.
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
Set, then, the third parameter of html_entity_decode(), charset, to 'UTF-8' (the default value).
PS: Please note that setting the encoding of the page to UTF-8 will solve many issues, but may introduce others, since UTF-8 can use multibyte characters. There may be security issues too, if you fail to validate input data correctly.
html_entity_decode($test, ENT_COMPAT, 'UTF-8');
There are other charsets too:
html_entity_decode($test, ENT_COMPAT, 'ISO-8859-15');
You need to define with which charset you're working with.
You will have to use the third parameter of html_entity_decode to specify a character set.
$test = html_entity_decode($test, ENT_COMPAT, 'utf-8');

Categories