Page 111 - Open Soource Technologies 304.indd
P. 111
Unit 5: Strings
$string1 = “PRADIP kumar”; $string2 = “barney rubble”; print(strtolower($string1)); Notes
print(strtoupper($string1)); print(ucfirst($string2)); print(ucwords($string2)); Pradip kumar
PRADIP KUMAR Barney rubble Barney Rubble
If you have got a mixed-case string that you want to convert to “title case,” where the first letter
of each word is in uppercase and the rest of the letters are in lowercase, use a combination of
strtolower( ) and ucwords( ):
print(ucwords(strtolower($string1))); Pradip Kumar.
Create a PHP code for making first letter caps to enter string.
5.5 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.
5.5.1 HTML
Special characters in HTML are represented by entities such as & and <. There are two PHP
functions for turning 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 htmlspecialchars( ) 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.
Example:
$string = htmlentities(“Einsturzende Neubauten”); echo $string; Einstürzende Neubauten
The entity-escaped version (ü) correctly displays as ü in the 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 character set. The default is “ISO-8859-1”. The
quote_style parameter controls whether single and double quotes are turned into their entity
forms. ENT_COMPAT (the default) converts only double quotes, ENT_QUOTES converts both
types of quotes, and ENT_NOQUOTES converts neither. There is no option to convert only single
quotes. For example:
$input = <<< End “Stop pulling my hair!” Jane’s eyes flashed.<p> End; $double =
htmlentities($input); // "Stop pulling my hair!" Jane’s eyes flashed.<p> $both
= htmlentities($input, ENT_QUOTES); // "Stop pulling my hair!" Jane's eyes
flashed.<p> $neither = htmlentities($input, ENT_NOQUOTES); // “Stop pulling my hair!”
Jane’s eyes flashed.<p>
LOVELY PROFESSIONAL UNIVERSITY 105