PHP: echo £ sign before a variable displays an unwanted character - php

echo "£$price";
Displays:
£35
How can I get rid of that unwanted char?

First you have to convert that character to an html entity.
echo htmlentities('£').$price;

Sounds like you don't serve your content as utf-8.
Do this by setting the correct header :
header('Content-type: text/html; charset=utf-8');
In addition to be really sure the browser understands, add this HTML Meta tag in your page:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Or instead of £ with HTML output you can always write
£

Your editor is probably set to save your files in utf8, whcih is fine.
The browser may use a different encoding by default. You can hint te browser by using this html5 meta tag:
<meta charset="utf8">
Or the older non-html5 equivalent
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
You can also specify the charset as a HTTP Header, using header():
header("Content-Type: text/html; charset=utf-8");
It also doesn't hurt to read Joel on charsets.

Related

PHP Turkish Language displaying issue

I have been trying to display my text in PHP but I am getting issue from Turkish characters it display it as ıııööö
I am reading data from MySQL but the issue is from the PHP. Please what it is missing?
My code is simple:
<?php
echo "ş i ü ğ";
?>
Check, if your website is in UTF-8.
Just copy following code to the <header> tag of your documment:
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
Or you can use just shorter version according to HTML5. Compatibility is fine:
<meta charset='utf-8'>
See difference between this two metatags: <meta charset="utf-8"> vs <meta http-equiv="Content-Type">
Or you can use header. This code place into your PHP code. The best position is at the start of the document.
header('Content-Type: text/html; charset=utf-8');
Or check if your file is in UTF-8. This you can do with your code editor. I use PSPad and i recommend it to try.
In addition to meta headers, the file's own character encoding is also important. I had suffered from the same issue with the files generated with notepad++ and notepad back in Windows, then I saved the file as UTF-8 and it all solved it.
E.g: with Sublime Text; file/save with encoding/UTF-8.

’ is being displayed instead of -

’ is being displayed instead of - in php page
I tried using different encoding types like:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
and
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
but result is the same. What could be the problem?
Input
<strong style="color:#A8A8A8;">1</strong> – Lorem Ipsum.
Result
1 – Lorem Ipsum.
Make sure your html header specifies utf8
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
That usually does the trick for me (obviously if the content IS utf8).
You don't need to convert to html entities if you set the content-type.
check http://php.net/manual/en/function.mb-convert-encoding.php
<?php
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
?>
may be this will help you.
It looks like your source data is converted from one to another encoding along the way. Try to make sure ALL steps have the same encoding.
Is your (MySQL?) data stored as UTF8?
Is your .php file saved as UTF8?
Conversion errors like this usually pop up when handling UTF8 data as ISO-8859-1 data. (multibyte vs singlebyte? not sure).
The fact that the meta tag doesn't change the output is a strong indicator that there's something overriding it; probably it's the charset specified in the HTTP header (which has precedence over the meta tag), are you sure you're not setting it there?
Your document is most likely encoded in UTF-8 since – is the iso-8859-1 presentation of the UTF-8 encoded character –.
What you need is the meta-tag you describe:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
Since it isn't working, the tag might to be ignored. Suggestion is to use the browser and check what encoding it tries to use (Tools - Encoding in Chrome).
If the browser uses UTF-8, you have double-encoded the characters. Check your code if so that you don't have an excessive utf8_encode(...)
If the browser uses Latin1 (iso-8859-1) your tag is ignored or overridden by the HTTP header. Try to validate your HTML with an online validator. Check the sent header information with your browser's development tool to make sure iso-8859-1 is not set as encoding.
Had the same problem when creating a file from javacode and setting the encoding to UTF-16 did the trick.

How can I print special HTML characters correctly, using PHP?

I'm trying to print a string, like this:
Noticias de Fútbol.
But when I print this string, it displays like this:
Noticias de F�tbol.
I tried htmlspecialchar() and many more, but my output remains the same.
$str = "Noticias de Fútbol";
$strfoot = html_entity_decode($str);
How can I resolve this?
You don't need to use html_entity_decode, you can just simply print out your $str with echo, but you have to make sure your html has this line of code in the <head>:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
You have use charset utf 8 and htmlentities function of php
Like
<meta http-equiv="content-type" content="text/html; charset=utf-8">
$test=htmlentities("Sisälämpötila");
echo $test;
You are most likely outputting to a page that has the wrong charset specified. To confirm this open a JavaScript console and evaluate window.document.charSet and window.document.characterSet.
If that is the case you can use a meta tag to inform the browser of your intended charset:
<meta content="text/html; charset=utf-8">
I'm reading elsewhere the tag might be:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
Your php file is not utf-8 but the browser is reading it as utf-8 which is giving you that question mark character. Save your file as utf-8, most editors/ides have options to set the charset of the file.

Special characters from xml file don't display correctly using php

This may be a stupid question but its not a matter of what I can find, its a matter that I dont know what to search for. There are some special characters that don't show correctly in php. I'm taking some information from an xml file, and echo-ing them.
ie:
should be -> Nürnberg
echoes as -> Nürnberg
any tips on what to look for, or how to resolve this?
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
You simply have an encoding mismatch. Get up to speed with these articles:
What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text
Handling Unicode Front To Back In A Web App
try a different character set on the page you're echoing from
http://www.w3schools.com/tags/ref_charactersets.asp
Can you try with following meta tag in your HTML head.
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
There is a mismatch between the character encoding of your XML and what you are outputting from PHP. Most likely, one is UTF-8 and one is ISO-8859.
On the PHP side, you can set this with a header directive
<?php
header('Content-Type: text/plain; charset=ISO-8859-1');
header('Content-Type: text/plain; charset=utf-8');
?>
and/or in the outputted HTML
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
On the XML side, most quality text editors allow you to specify the character encoding as you save the file. (E.g. WordWrangler on Mac)
If the XML file is indeed in ISO-8859, you could use utf8_encode() to convert it to UTF-8 as you read it in.
An in-depth discussion of PHP and character encoding.
"I'm taking some information from an xml file, and echo-ing them."
Windows command line doesn't support utf8 properly as it doesn't use an UTF8 font.
Just put the file into somewhere that's reachable through a web server and test it by calling the file through the web server.
Alternatively pipe the output of the script into a text file:
php test.php > output.txt
And either open output.txt is a UTF8 capable editor or use a utf8 capable 'Tail' program.
Test.php
<?php
echo "Nürnberg";
?>
Running from command prompt:
php test.php
Nürnberg
Calling through a web server http://localhost/test.php
Nürnberg

Web page text display problem

I keep getting these weird text characters when I display user submitted text. like in the following example below. Is there a way I can fox this using PHP, CSS or something so that the characters are displayed properly?
Here is the problem text.
Problems of �real fonts� on the web. The one line summary:
different browsers and different platforms do �hinting�
Here is my meta tag.
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
It's an encoding problem. Make sure you send the correct encoding to the browser. If it's UTF-8, you'll do it like this:
header("Content-type: text/html; charset=utf-8");
Also, make sure that you store the content using the same encoding throughout the entire system. Set your database tables to utf8. If you're using MySQL, run the SET NAMES utf8 query when connecting to make sure you're running in UTF-8.
These weird characters occur when you suddenly switch encoding.
Also, some functions in PHP take a $charset parameter (e.g. htmlentities()). Make sure you pass the correct charset to that one as well.
To make sure that PHP handles your charset correctly in all cases, you can set the default_charset to utf-8 (either in php.ini or using ini_set()).
Set your page to UTF-8 encoding.
Please check with the char-set in header section.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
use this below one:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
or try this one:
htmlentities($str, ENT_QUOTES);
Could be problem with file encoding please check that your files is correctly encoded, saved as "UTF-8 without boom", also if you are saving to database use SET NAMES UTF-8

Categories