11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 9 • STRINGS AND REGULAR EXPRESSIONSConverting Strings to and from HTMLConverting a string or an entire file into a form suitable for viewing on the Web (and vice versa) is easierthan you would think. The following functions are suited for such tasks.Converting Newline Characters to HTML Break TagsThe nl2br() function converts all newline (\n) characters in a string to their XHTML-compliantequivalent, . 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:// convert the newlines to 's.echo nl2br($recipe);Executing this example results in the following output:3 tablespoons Dijon mustard1/3 cup Caesar salad dressing8 ounces grilled chicken breast3 cups romaine lettuceConverting Special Characters to Their HTML EquivalentsDuring the general course of communication, you may come across many characters that are notincluded in a document’s text encoding, or that are not readily available on the keyboard. Examples ofsuch characters include the copyright symbol (©), the cent sign (¢), and the grave accent (è). To facilitatesuch shortcomings, a set of universal key codes was devised, known as character entity references. Whenthese entities are parsed by the browser, they will be converted into their recognizable counterparts. Forexample, the three aforementioned characters would be presented as ©, ¢, and È,respectively.To perform these conversions, you can use the htmlentities() function. Its prototype follows:string htmlentities(string str [, int quote_style [, int charset [, boolean double_encode]]])211

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!