I am trying to create an array with Danish characters - why are the characters converted to UTF-8 when output by PHP? Apache's httpd.conf? PHP.ini?
// Fails
$chars = array_merge(range("A","Z"),str_split("ÆØÅ"));
// Observed result: (array) ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ
// Expected result: (array) ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ
// Works
$chars = array_merge(range("A","Z"),str_split(utf8_decode("ÆØÅ")));
// Observed result: (array) ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ
I have tried to set Content Type and Default Charset to ISO-8859-1 in the document top:
header('Content-type: text/html; charset=ISO-8859-1');
ini_set('default_charset', 'ISO-8859-1');
Content Type is also set in the HTML document (while this is not relevant since the issue occurs in the PHP engine, before HTML is output):
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Sorry about answering my own question..
I solved this by changing the file encoding from UTF-8 to ANSI.
Related
I am trying to scrape a webpage in arabic and everything works fine except the fact that when i echo the text what i get is a garbled up text even though i have set the header to UTF-8
Here is my code
<?php
header ('Content-Type: text/html; charset=UTF-8');
require 'vendor/autoload.php';
use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://www.lebanonfiles.com');
$news_container = $crawler->filter('#mcs4_container .line');
$news_container->each(function($node) {
echo $node->text();
})
?>
What i get is this piece of garbled text
You should try this... try to put this line at beginning of your php file: ini_set('default_charset', 'UTF-8'); this may solve your issue.
Have a nice day.
ALL attributes must be set to UTF-8, on all levels of your application/script.
Save the document as UTF-8 or as UTF-8 w/o BOM (If you're using Notepad++, it's Format -> Convert to UTF-8)
Note that even though they are both UTF-8, they can behave differently!
The header in both PHP and HTML should be set to UTF-8
HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
PHP: header('Content-Type: text/html; charset=utf-8');
You may need to specify your charset in your php.ini file, using default_charset = "utf-8", although this is standard in PHP 5.6
Everything that can be set to a specific charset, should be set to the same.
There might be different aspects of your code that needs to be set to a specific charset.
I'm pulling the Facebook meta tags from an external site with this code, which works:
$site = file_get_contents($link);
$html = new DOMDocument();
#$html->loadHTML($site);
$meta_title = null;
foreach($html->getElementsByTagName('meta') as $meta) {
if($meta->getAttribute('property')=='og:title'){
$meta_title = $meta->getAttribute('content');
}
}
echo 'og:title: '.$meta_title;
My problem is that if the og:title contains something with an apostrophe, for example, it outputs a bunch of funky characters. For example:
That’s the Spot
Instead of:
That's the Spot
How do I make it output correctly?
Check the third part website collation, it was on utf-8 or latin.
Then you should convert to your website collation. What are you using? utf8 or latin?
If you are using utf8 and third part latin, you should use
utf8_encode($actualVar)
If you are using latin and the third part utf8, you should use
utf8_decode($actualVar)
I suppose there are 2 different collations ruling it. If Are The Two UTF8 Convert your php header to utf8 too:
header('Content-Type: text/html; charset=utf-8');
If you are trying to use Latin (iso-8859-1) Use
header('Content-Type: text/html; charset=iso-8859-1');
By the wall should work any way.
The solution was including the UTF-8 encoding meta tag at the top of the PHP file.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I have the following text which I manually enter into the wordpress posts table
‚
I encode into utf-8 using:
$text = "‚";
$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;
There is a MySQL database containing data with accentuated letters like é. I want to display it in my PHP page , but the problem is that there are unrecognized characters displayed because of the accent. So is there a function to convert accent to HTML code , for example é is converted to é !
Rather than using htmlentities you should use the unicode charset in your files, e.g.
<?php
header('Content-Type: text/html; charset="utf-8"');
To be on the safe side, you can add the following meta tag to your html files:
<html>
<head>
<meta charset="utf-8" />
Then, make sure that your data base connection uses utf8:
mysql_connect(...);
mysql_select_database(...);
mysql_set_charset('utf-8');
Then, all browsers should display the special characters correctly.
The advantage is that you can easily use unicode characters everywhere in your php files - for example the copyright sign (©) or a dash (–) - given that your php files are encoded in utf-8, too.
Try htmlspecialchars() and/or htmlentities()
you can easily make one yourself with str_replace:
function txtFormat($input){
$output = str_replace('/\à/','É',$input);
$output = str_replace('/\è/','è',$output);
$output = str_replace('/\é/','é',$output);
$output = str_replace('/\ì/','ì',$output);
$output = str_replace('/\ò/','ò',$output);
$output = str_replace('/\ù/','ù',$output);
return $output;
}
Use the following code
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
And don't encode your data via utf8_encode() function before inserting into database
hope this will solve your problem :)
When I try and execute this code to print out an Arabic string: print("إضافة"); I get this output: إضاÙØ©. If I utf8_decode() it I'll get ?????. I have "AddLanguage ar" in my apache configuration but it doesn't help. How do i print out this Arabic string?
Also set your page language to utf8 eg:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and then see it if worked. If that still doesn't work, go and check this out, it is complete solution for the arabic language using PHP:
http://www.ar-php.org/en_index_php_arabic.html
You may want to check out this too:
http://www.phpclasses.org/browse/package/2875.html
It might be necessary to indicate to the browser which charset you are using -- I'm guessing it's UTF-8.
IN order to achive that, you might try putting this portion of code at the beginning of your script, before any output is generated :
header('Content-type: text/html; charset=UTF-8');
[utf8_decode][1] will try to decode your string from UTF-8 to latin1, which is not suited for Arabic characters -- hence the '?' characters.
You may want to set
default_charset = "utf-8"
in your php.ini. Default charset directive instructs the server to produce correct content type header.
You can also do it in runtime:
ini_set('default_charset', 'utf-8');
You may also want to check your browser font if it has Arabic support. Stick to common fonts like Arial Unicode and Times New Roman.
Well,
First: Add by the beginning of HTML page
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
second:
if you are using AJAX encode data using encodeURIComponent
Third:
First line of your PHP page should be
header('Content-Type: text/html; charset=utf-8');
and decode the data sent to PHP using urldecode
Regards,