Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi again Larry,

 

Character encoding....Hmmmm

 

If I write HTML, and want to include an ampersand -> & - or a euro sign -> € or alternatively € I type exactly those characters. 

 

The question is when one is using php to write text to mySQL.  For example, if you have a web page which invites a customer to upload text to the database.  If they use a keyboard that has unusual characters such as a half, which I would type in HTML as ½ it is not saved as such even though the DB has UTF-8 encoding.  So...what happens when the text is retrieved from the DM and sent to the browser is that you get the ?inside a black diamond symbol.

 

I have read your book a milion times (as well as your excellent javascript tome) and just can't get my head round it.

 

I suppose I could write a load of turgid php such as 'str_replace....' for each character we are likely to come across.

 

Any advice would be much appreciated.

 

Interestingly, the Daily Mail website forum has the same problem, which doesn't make me feel any better, to be honest!!

 

Thanks again for all your time spent on us dunces!

 

P.S.  Another question - how would you offer the client the option to put into text, say, a superscript 2 in the sense of metres squared - or in the USA - feet squared when there is no key for that?

 

Sorry for the spelling in the title!!

Edited by Max
Link to comment
Share on other sites

Hey Max! Thanks for the nice words on the books! I really appreciate it. If I'm understanding your question correctly, the answer is to make sure you're using UTF-8 everywhere for the input: the page itself (in your text editor/IDE), the HTML, the PHP connection to the database, and in the database itself. Then, when you go to display stored text, you'll want to use htmlentities() to convert special characters to their entity equivalent, safe to use in HTML. 

 

As for superscript...I don't know. My guess is the person would have to use Insert Special Characters (or their OS equivalent). 

Link to comment
Share on other sites

Hi Larry
 
Comments genuinely meant - and if I am mad enough to scramble my brains by learning something new I will always look for your books on that subject.
 
Regarding the character encoding, I am referring to a textarea such as this one.  Head is set to: <meta charset="utf-8"> and the MySQL "utf8_unicode_ci".  On my keyboard are characters such as an "enye" - the n with a tilde on top.  Also, it would be nice for people to have a way of writing mas for 'square metres'.  
 
When I put the 'enye' into MySQL using phpMyAdmin it works and reads correctly, but when read by html in my web page it comes out as '�'.
 
I guess the obvious thing for me to do is use the same software as is used on this forum which looks like "ipsEditor" (Invision Power).  For example, you have X2 and X2 on the control panel.
 
To keep it simple, I guess that I could put instructions under the textarea field saying "To insert superscript 2 type ' ² '" as most people don't have superscript 2 on their keyboard anyway.

 

Here's a quick and dirty program I wrote to test it:

 

<!doctype html>

<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
 
<meta charset="utf-8">
<!--[if lt IE 9]>
   <![endif]-->
</head>
<body>
<?php
 
echo '<h2>';
echo 'This is the raw data: Ñ';
echo '<br /><br /><br />';
echo 'This is using HTML entities:  Ñ';
echo '<br /><br /><br />';
$ent = "Ñ";
echo 'This is using htmlentities:  ' . htmlentities($ent);
echo '</h2></body></html>';
 
If I run this on the server, I get the following:
 
This is the raw data: �


This is using HTML entities: Ñ


This is using htmlentities:

Best regards

 

Max

Link to comment
Share on other sites

In addition, I have been caught out with uploading text to MySQL from a textarea - when people use an apostrophe - as in "I've" - then the MySQL sees that apostrophe as the end of a data set as in - UPDATE blah SET Comments = 'blah blah it's blah blah' you find that MySQL has thrown it all out because it has seen the " ' " as a termination character.  The problem is that there are so many apostrophes on a keyboard - ' , ` , ´ , " etc - so I have included a str_replace routine to turn these into spaces as \' doesn't seem to work in all circumstances.

Link to comment
Share on other sites

Following your book, I have now added mysqli_real_escape_string() to the php which actually works for everything except `` and ´´ which it doesn't escape, but I will take the risk of those never being used by the people who are uploading data.  They are not the standard apostrophes but Hex 60.

 

To get to the root of the problem, what I did was to comment out the steps whereby I uploaded the data to MySQL and instead did an 'echo' instead:

 

e.g.  

 

$update = "UPDATE blah SET Name = '$name', Address = '$address', Description = '$desc' WHERE Ref = $ref LIMIT 1";

echo $update;

 

I then ran the script and copy/pasted the result from the screen into the phpMyAdmin 'SQL' service and found that it failed.  On investigation, it was the apostrophe causing th problem.

 

I hope that this helps others.

Link to comment
Share on other sites

 Share

×
×
  • Create New...