Home arrow PHP arrow Page 3 - Converting Strings and Regular Expressions

Creating a Customized Conversion List - PHP

In this third part of a five-part series on strings and regular expressions in PHP, you will learn how to convert strings to and from HTML, and more. This article is excerpted from chapter nine of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

  1. Converting Strings and Regular Expressions
  2. Using Special HTML Characters for Other Purposes
  3. Creating a Customized Conversion List
  4. Alternatives for Regular Expression Functions
By: Apress Publishing
Rating: starstarstarstarstar / 2
July 01, 2010

print this article



Thestrtr()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 (<b>) character to its XHTML equivalent:

    $table = array("<b>" => "<strong>", "</b>" => "</strong>");
    $html = "<b>Today In PHP-Powered News</b>";
    echo strtr($html, $table);

This returns the following:

<strong>Today In PHP-Powered News</strong>


Converting HTML to Plain Text

You may sometimes need to convert an HTML file to plain text. You can do so using thestrip_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 optionalallowable_tagsparameter allows you to specify which tags you would like to be skipped during this process. This example usesstrip_tags()to delete all HTML tags from a string:

$input = "Email <a href='spammer@example.com'>spammer@example.com</a>";
    echo strip_tags($input);

This returns the following:


The following sample strips all tags except the<a>tag:

    $input = "This <a href='http://www.example.com/'>example</a>
              is <b>awesome</b>!";
    echo strip_tags($input, "<a>");

This returns the following:

This <a href='http://www.example.com/'>example</a> is awesome!


Note  Another function that behaves like strip_tags()isfgetss(). This function is described in Chapter 10.  

>>> More PHP Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: