Prev | Current Page 324 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Assume that instead of printing the result of strtr() in the preceding
code sample, you assign it to the variable $translated_string.
The next example uses array_flip() to return a string back to its original value:
$entities = get_html_translation_table(HTML_ENTITIES);
$translate = array_flip($entities);
$string = "La pasta é il piatto piú amato in Italia";
echo strtr($string, $translate);
?>
CHAPTER 9 ?–  S TRINGS AND REGULAR EXPRESS IONS 259
This returns the following:
La pasta ?© il piatto pi?? amato in italia
Creating a Customized Conversion List
The strtr() function converts all characters in a string to their corresponding match
found in a predefined array. Its prototype follows:
string strtr(string str, array replacements)
This example converts the deprecated bold () character to its XHTML equivalent:
$table = array("" => "", "" => "");
$html = "Today In PHP-Powered News";
echo strtr($html, $table);
?>
This returns the following:
Today In PHP-Powered News
Converting HTML to Plain Text
You may sometimes need to convert an HTML file to plain text. You can do so using the
strip_tags() function, which removes all HTML and PHP tags from a string, leaving
only the text entities. Its prototype follows:
string strip_tags(string str [, string allowable_tags])
The optional allowable_tags parameter allows you to specify which tags you would
like to be skipped during this process.


Pages:
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336