PHP UK £ symbol inconsistency - php

I have 2 similar PHP pages, one displays the UK £ symbol correctly, the other displays a black diamond with a ? in it.
In order to diagnose the problem I cut the code down and they are now identical, but still display differently. How can that be???
This is the code
`
<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
</head>
<body>
<?php
echo '<p>';
echo "£"."123";
?>
</body>
</html>`
While working with the original code it seemed that I could fix one by removing the charset=utf8 but if I removed it from the other it prefixed the £ with a capital A with an accent.
What is happening here?

You have configured another charset in your apache configuration. Maybe your php are proccessed with ISO-8859-1 and you are defining in your HTML UTF-8. That's an inconsistency. Try to define UTF-8 in your apache configuration.
See this post:
How to change the default encoding to UTF-8 for Apache?
In httpd.conf add (or change if it's already there):
AddDefaultCharset utf-8

The reason the issue was occurring was that the PHP files were saved with different encoding. The one which behaved okay had encoding UTF-8, while the problematic file had encoding Windows-1252. I use Bluefish and it all looked okay in there, but when I ran the cat command on the file I could see the odd character.
Thanks for your help!

Related

How to make sure that php files are actually saved with utf-8 encoding?

I'm running into an annoying php utf8 netbeans encoding problem. I spent all night yesterday searching Google and also StackOverflow for solutions. I tried many things and it seems I can't get it to work. I know i'm doing something wrong...
I have a php project that needs to print french caracters like é è ç à but instead of the letter it outputs �
I added this to my file:
<!DOCTYPE html>
<html>
<?php
header('Content-type: text/html; charset=utf-8');
?>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
</head>
Some users suggested to modify neatbeans.confby adding -J-Dfile.encoding=UTF-8 on this line netbeans_default_options
Still the problem remains...
I right clicked on the project -> properties -> sources and my encoding is set to windows-1252 I tried to change it to UTF-8 but it gives me this warning : Changing to UTF-8 may cause some files to be unreadable. Would you like to continue>
I clicked yes but the problem remains...
So how can I make sure my file is encoded in UTF-8? Easy way please.
What I have done to resolve this kind of issue is using:
iconv("ISO-8859-1", "UTF-8", $value);

Convert utf8 without bom to utf 8

Files
index.php :
<?php
include_once 'index_a.php';
?>
index_a.php :
<html>
<head>
<title>test</title>
</head>
<body>
casa
</body>
</html>
Results
The first result is from the index.php and the second index_a.php.
Why I defend those quotes?
If index_a.php converts the file in UTF-8 without BOM, quotation marks do not appear, but I want the file to be encoded in UTF-8.
you question doesn't make sense: UTF8 file encoding may (but shouldn't, as the byte ordering for UTF8 is fixed) have a BOM. In both cases your file will be UTF8 encoded, so you're done already. What happened here is that you've asked an XY question
So, what you really want to know is: why do those quotes show up for a normal UTF8 encoded file without BOM, but not when there is a BOM, and the answer to that is that you're giving the browser HTML code that could be any version of HTML, and expect it know which version you want rendered.
Without any knowledge of the document type, the browser may, or may not, treat any whitespace between tags as a single whitespace, or no whitespace, depending on the rendermode it guessed you wanted. So if you really don't want that " " then you shouldn't rely on the file encoding, you should make it explicit to the browser that what you're giving it to render is proper HTML. Add
<!doctype html>
at the top so that all browsers know this is a modern HTML5 content file and should be parsed accordingly, rather than falling back into an unpredictable quirks mode.
edit
http://jsbin.com/helikafuni/1/ shows proper HTML5 doctype and element use (you're using ancient HTML4.1 syntax. It's time to read up on how HTML5 changed a lot of the rules and use those new rules instead)
If you want to change your encoding of your Files i would sugguest you to use Notpad++!
After you installed it you can open your files in it and change the encoding like this:
(See point "Convert to UTF-8")
UPDATE:
This should work for you:
index.php:
<?php
include_once 'index_a.php';
?>
index_a.php:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
casa
</body>
</html>

How to save Russian characters in a UTF-8 encoded file

OK so I have a PHP file with several strings of text in various languages. For most languages like French or Spanish I just simply type in the characters.
The problem I have is with Russian language characters. The PHP file is encoded in UTF-8, how can I make sure that the Russian characters are both saved correctly and displayed correctly on the output web page... Is it just a case of pasting the text into the PHP file, or is there a way to guarantee the characters will be saved into the file correctly - perhaps converting it into HTML-like notation for example?
Obviously I am assuming the end user will have the correct encoding set in their web browser, I just want to make sure I got it all covered from my end.
I am using Notepad++ on Windows to edit my PHP file.
Thanks!
If you want to tell browsers your encoding, place it inside your <header> tag:
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
Or short version
<meta charset='utf-8'>
That should be pretty enough for Russian characters to be correctly displayed on a webpage.
if your doctype is html declare <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'> but if your doctype is xhtml then declare <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />.Never assume that end-user will act correctly during your designsIf you already have some document, edit your document's meta tag for charset declaration and use notepad++ encoding>convert to UTF-8 without BOM, save your document, safely go on with your multilingual structure from now on.php tag is irrelevant for your question since you don't mention about any database char setting.
There is no difference between Latin and Cyrillic characters in UTF-8. Both are just byte sequences. Configure your server or PHP script to send Content-Type: text/html;charset=utf, and you are rather safe.
Your editor might have problems when the font you are using does not contain Russian characters. Choose another font then.
And please ignore the <meta> element recommendations. You don't need that: it is useless when your HTTP headers are correct, and maybe harmful if they aren’t.
Well you have to check 2 things
To ensure that *.php is an UTF-8 file I use PSPad. If file is not in UTF-8, I save
it like that: http://stepolabs.com/upload/utf-8.png
Then your website must have UTF-8 encoding in <meta> tag;
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
... more about metatagging.
Finally if everything is done well - (format and meta declaration) all should be displayed properly!

Differing outputs depending on where one is accessing a page from?

I'm having a weird problem regarding PHP and charset encoding..
I am developing a PHP Page that outputs some latin (ã, á, à, and so on) characters.
When I run the page from my localhost, it works just fine. However, when I upload it to my test-server and access it via a url, then all latin characters become little squares (in IE by the way).
I've changed the character encoding on my browser back and forth to utf8, western european and so on, but none seem to work
Does anybody have an idea?
Either set the default_charset in php.ini to UTF-8, or if you don't have the control over this, add the following line to the top of the PHP file, before you emit any character to the response body:
header('Content-Type: text/html;charset=UTF-8');
See also:
PHP UTF-8 cheatsheet
Have you checked for different default_charset settings in the php.ini files of your localhost and your test server?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> // Or actual encoding
</head>

Browser displays � instead of ´

I have a PHP file which has the following text:
<div class="small_italic">This is what you´ll use</div>
On one server, it appears as:
This is what you´ll use
And on another, as:
This is what you�ll use
Why would there be a difference and what can I do to make it appear properly (as an apostrophe)?
Note to all (for future reference)
I implemented Gordon's / Gumbo's suggestion, except I implemented it on a server level rather than the application level. Note that (a) I had to restart the Apache server and more importantly, (b) I had to replace the existing "bad data" with the corrected data in the right encoding.
/etc/php.ini
default_charset = "iso-8859-1"
You have to make sure the content is served with the proper character set:
Either send the content with a header that includes
<?php header("Content-Type: text/html; charset=[your charset]"); ?>
or - if the HTTP charset headers don't exist - insert a <META> element into the <head>:
<meta http-equiv="Content-Type" content="text/html; charset=[your charset]" />
Like the attribute name suggests, http-equiv is the equivalent of an HTTP response header and user agents should use them in case the corresponding HTTP headers are not set.
Like Hannes already suggested in the comments to the question, you can look at the headers returned by your webserver to see which encoding it serves. There is likely a discrepancy between the two servers. So change the [your charset] part above to that of the "working" server.
For a more elaborate explanation about the why, see Gumbo's answer.
The display of the REPLACEMENT CHARACTER � (U+FFFD) most likely means that you’re specifying your output to be Unicode but your data isn’t.
In this case, if the ACUTE ACCENT ´ is for example encoded using ISO 8859-1, it’s encoded with the byte sequence 0xB4 as that’s the code point of that character in ISO 8859-1. But that byte sequence is illegal in a Unicode encoding like UTF-8. In that case the replacement character U+FFFD is shown.
So to fix this, make sure that you’re specifying the character encoding properly according to your actual one (or vice versa).
To sum it maybe up a little bit:
Make sure the FILE saved on the web server has the right encoding
Make sure the web server also delivers it with the right encoding
Make sure the HTML meta tags is set to the right encoding
Make sure to use "standard" special chars, i.e. use the ' instead of ´of you want to write something like "Luke Skywalker's code"
For encoding, UTF-8 might be good for you.
If this answer helps, please mark as correct or vote for it. THX
The simple solution is to use ASCII code for special characters.
The value of the apostrophe character in ASCII is ’. Try putting this value in your HTML, and it should work properly for you.
Set your browser's character set to a defined value:
For example,
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
This is probably being caused by the data you're inserting into the page with PHP being in a different character encoding from the page itself (the most common iteration is one being Latin 1 and the other UTF-8).
Check the encoding being used for the page, and for your database. Chances are there will be a mismatch.
Create an .htaccess file in the root directory:
AddDefaultCharset utf-8
AddCharset utf-8 *
<IfModule mod_charset.c>
CharsetSourceEnc utf-8
CharsetDefault utf-8
</IfModule>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Categories