As a programmer, when you deal with database issues and CSV you often come up with issues where you construct HTML or read HTML as part of the final solution, and you have to deal with the ambiguity of ‘ and , characters, respectively, having dual purposes. The ‘ delimits characters in SQL for the databases MySql, MS-SQL, Oracle, Access and others. The , character is the default delimiter for CSV data and lists of things that you “split” or “explode” into an array. But this can be problematic, in that ‘ and , can also naturally exist in a person’s name, or their address, for instance.
This is the situation where HTML entities can be very useful, as they can replace your delimiters (‘ and , are just two examples, but the method can apply to others as well) with what is known as HTML entities, to avoid any data confusion for the use of this data by Javascript and/or PHP or any other client/server-side language combinations you are using.
Let’s take a look at Wikipedia’s write up regarding HTML entities.
In SGML, HTML and XML documents, the logical constructs known as character data and attribute values consist of sequences of characters, in which each character can manifest directly (representing itself), or can be represented by a series of characters called a character reference, of which there are two types: a numeric character reference and a character entity reference. This article lists the character entity references that are valid in HTML and XML documents.
Although in popular usage character references are often called “entity references” or even “entities”, this usage is wrong.[citation needed] A character reference is a reference to a character, not to an entity. Entity reference refers to the content of a named entity. An entity declaration is created by using the syntax in a document type definition (DTD) or XML schema. Then, the name defined in the entity declaration is subsequently used in the XML. When used in the XML, it is called an entity reference.
Now let’s see a good link listing a lot of the HTML entities of use here.
As the programmer confronted with such issues Javascript’s replace and PHP’s str_replace come in really handy.
Example code snippets are shown below:
str_replace("'", "’", $myPHPvar);
// replace all ‘ with its HTML entity in PHPstr_replace(",", ",", $myPHPvar);
// replace all HTML entity occurrences back to , in PHPmyJavascriptVar.replace(/,/g, ",");
// replace all , with its HTML entity in JavascriptmyJavascriptVar.replace(/’/g, "'");
// replace all HTML entity occurrences back to the original ‘ character in Javascript
Of course, this is like a “first principles” talk above, but, as you can imagine with such a fundamental subject, the languages have catered well for the thousands of other ways “to skin a cat” … meeeeeeeeeeeeoooooooowwwwww! … sorry, kitty … and we’ll leave you with some good links here … so, chou for now … tee, hee …
|| chortle, chortle.
Here is a Yahoo Answers question regarding this topic area … What is » used when coding?
If this was interesting you may be interested in this too.
5 Responses to HTML Entities Primer Tutorial