Skip to main content

HTML Conversion



PHP's strength is in its ability to turn a static web page into a dynamic web page by interacting directly with
the HTML. Part of this strength is PHP's ability to convert non-HTML (text only) strings to HTML and
HTML strings to non-HTML strings. To make this happen, PHP provides multiple built-in functions
designed specifically for HTML conversions. The following is a list of the most commonly used:
Text to HTML Conversion Functions

nl2br -
            This function name (nl2br) is an acronym for "newline to <br /> tags" due to the fact that this
function will convert all newline characters (\n) in strings to their XHTML-compliant equivalent (<br
/>).


<?php
$text = "See Spot.
See Spot run.
See Spot fall.
See Spot's friends laugh.
Hear Spot growl.
See Spot's friends run.";
print nl2br($text);
// The sentence will now read "See Spot.<br />See Spot run.
// <br />See Spot fall.<br />See Spot's friends laugh.<br />
// Hear Spot growl.<br />See Spot's friends run."
?>

htmlspecialchars -
                             There are numerous characters that HTML identifies as special for completing
certain tasks that are everyday characters in written language. For example; the ampersand sign (&)
is used in many languages to abbreviate the word "and"; however, the ampersand sign is also used in
HTML as a special character. If a programmer wants to actually use the ampersand sign as its
original meaning, the following HTML code must be entered in its place: "&amp" as in "Jack &amp
Jill". The PHP function htmlspecialchars() will convert all such special characters in HTML to their
HTML equivalents.

<?php
$text="<<Storytime>> Jack & Jill went up the hill ... Jack
fell
down! Jill screamed \"Jack are you OK?\"";
print htmlspecialchars($text);
// The sentence will now read "&lt;&lt;Storytime&gt;&gt; Jack
// &amp Jill went up the hill ... Jack fell down! Jill
screamed
// &quot;Jack are you OK?&quot;"
?> 

If using both the nl2br() and htmlspecialchars() functions against the same string, it is important to
execute the htmlspecialchars() first and then nl2br() second. If the order is reversed, the XHTMLequivalent
line returns (<br />) will be converted to &lt;br /&gt; defeating the purpose of the
functions.


strtr -
               This function uses arrays to handle search and replace operations. It is similar in nature to the
preg_replace() Perl-style regular expression function. The following is an example presented earlier
using the preg_replace() function:


<?php
$text = "The dog barks, the bear growls and the cat meows";
$adults = array("/dog/", "/bear/", "/cat/");
$children = array("puppy", "cub", "kitten");
print preg_replace($adults,$children, $text);
?>

The following is an example of using the strtr() function to recreate the above example:

<?php
$text = "The dog barks, the bear growls and the cat meows";
$translation_table = array("dog" => "puppy", "bear" => "cub",
"cat" => "kitten");
print strtr($text, $translation_table);
?>

With this capability, there are many uses that the strtr() function can provide to the programmer.
However, due to the fact that the discussion now revolves around converting strings to HTML, it is
time to talk about the get_html_translation_table() function.

get_html_translation_table -
                                             PHP has built in arrays for handling the most common HTML
equivalent characters. The arrays are referenced by calling them with the function
get_html_translation_table(). The two arrays that are available are HTML_SPECIALCHARS and
HTML_ENTITIES. The following examples demonstrate how these two arrays are called and
utilized in PHP code:

<?php
$text="<<Storytime>> Jack & Jill went up the hill ... Jack fell
down! Jill screamed \"Jack are you OK?\"";
$translation_table=get_html_translation_table(HTML_SPECIALCHARS);
print strtr($text, $translation_table);
// The sentence will now read "&lt;&lt;Storytime&gt;&gt; Jack
// &amp Jill went up the hill ... Jack fell down! Jill screamed
// &quot;Jack are you OK?&quot;"
?>

<?php
$text="Júan and Felípe paid 13¢ a piece for gum.";
$translation_table = get_html_translation_table(HTML_ENTITIES);
print strtr($text, $translation_table);
// The sentence will now read "J&uacute;an and Fel&iacute;pe
//paid 13&cent; a piece for gum."
?>

To see what characters are in the HTML_ENTITIES translation tables, use the following PHP code
(view the source of the web page to see the HTML special characters):

<?php
$translation_table = get_html_translation_table(HTML_ENTITIES);
foreach($translation_table as $row) print "$row<br>\n";
?>

Comments

Popular posts from this blog

PHP INTRODUCTION

                     PHP  (recursive acronym for  PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP stands for  P HP:  H ypertext  P reprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource:  www.php.net PHP is easy to learn and runs efficiently on the server side What can PHP do? Anything. PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynam

MySQL General Architecture

        MySQL operates in a networked environment using a client/server architecture. In other words, a central  program acts as a server, and various client programs connect to the server to make requests. A MySQL  installation has the following major components: MySQL Server, Client programs and MySQL non client  utilities.  MySQL Server MySQL Server, or mysqld, is the database server program. The server manages access to the actual  database (schema) on disk and in memory. MySQL Server is multi-threaded and supports many  simultaneous client connections. Clients can connect via several connection protocols. For managing  database contents, the MySQL server features a modular architecture that supports multiple storage engines  that handle different types of tables (for example, it supports both transactional and non-transactional  tables). Keep in mind the difference between a server and a host. The server is software (the MySQL server  program mysqld). Server characteristi