Prev | Current Page 320 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


Converting Strings to and from HTML
Converting a string or an entire file into a form suitable for viewing on the Web (and
vice versa) is easier than you would think. Several functions are suited for such tasks,
all of which are introduced in this section.
Converting Newline Characters to HTML Break Tags
The nl2br() function converts all newline (\n) characters in a string to their XHTMLcompliant
equivalent,
. Its prototype follows:
string nl2br(string str)
The newline characters could be created via a carriage return, or explicitly written
into the string. The following example translates a text string to HTML format:
CHAPTER 9 ?–  S TRINGS AND REGULAR EXPRESS IONS 255
$recipe = "3 tablespoons Dijon mustard
1/3 cup Caesar salad dressing
8 ounces grilled chicken breast
3 cups romaine lettuce";
// convert the newlines to
's.
echo nl2br($recipe);
?>
Executing this example results in the following output:
3 tablespoons Dijon mustard

1/3 cup Caesar salad dressing

8 ounces grilled chicken breast

3 cups romaine lettuce
Converting Special Characters to Their HTML Equivalents
During the general course of communication, you may come across many characters
that are not included in a document??™s text encoding, or that are not readily available
on the keyboard. Examples of such characters include the copyright symbol (?©), the
cent sign (??), and the grave accent (??).


Pages:
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332