Encoding and Escaping
Because PHP programs often interact with HTML pages, web addresses
(URLs), and databases, there are functions to help you work with those
types of data. HTML, web page addresses, and database commands are all
strings, but they each require different characters to be escaped in
different ways. For instance, a space in a web address must be written as
%20, while a literal less-than sign
(<) in an HTML document must be
written as <. PHP has a number
of built-in functions to convert to and from these encodings.
HTML
Special characters in HTML are represented by
entities such as & and <. There are two PHP functions that
turn special characters in a string into their entities: one for
removing HTML tags, and one for extracting only meta tags.
Entity-quoting all special characters
The htmlentities()
function changes all characters with HTML entity equivalents into
those equivalents (with the exception of the space character). This
includes the less-than sign (<),
the greater-than sign (>), the
ampersand (&), and accented
characters.
For example:
$string=htmlentities("Einstürzende Neubauten");echo$string;EinstürzendeNeubauten
The entity-escaped version (ü—seen by viewing the source)
correctly displays as ü in the rendered web page. As you can see, the space has not been turned into
.
The htmlentities() function
actually takes up to three arguments:
$output = htmlentities(input,quote_style,charset);
The charset parameter, if given, identifies the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access