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

Alternatives for Regular Expression Functions - 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).

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

When youíre processing large amounts of information, the regular expression functions can slow matters dramatically. You should use these functions only when you are interested in parsing relatively complicated strings that require the use of regular expressions. If you are instead interested in parsing for simple expressions, there are a variety of predefined functions that speed up the process considerably. Each of these functions is described in this section.

Tokenizing a String Based on Predefined Characters

Thestrtok()function parses the string based on a predefined list of characters. Its prototype follows:

string strtok(string str, string tokens)

One oddity aboutstrtok()is that it must be continually called in order to completely tokenize a string; each call only tokenizes the next piece of the string. However, thestrparameter needs to be specified only once because the function keeps track of its position instruntil it either completely tokenizesstror a newstrparameter is specified. Its behavior is best explained via an example:

<?php
    $info = "J. Gilmore:jason@example.com|Columbus, Ohio";

    // delimiters include colon (:), vertical bar (|), and comma (,)
    $tokens = ":|,";
    $tokenized = strtok($info, $tokens);

    // print out each element in the $tokenized array
    while ($tokenized) {
        echo "Element = $tokenized<br>";
        // Don't include the first argument in subsequent calls.
        $tokenized = strtok($tokens);
    }
?>

This returns the following:

--------------------------------------------
Element = J. Gilmore
Element = jason@example.com
Element = Columbus
Element = Ohio
--------------------------------------------

Exploding a String Based on a Predefined Delimiter

Theexplode()function divides the stringstrinto an array of substrings. Its prototype follows:

array explode(string separator, string str [, int limit])

The original string is divided into distinct elements by separating it based on the character separator specified byseparator. The number of elements can be limited with the optional inclusion oflimit. Letís useexplode()in conjunction withsizeof()andstrip_tags()to determine the total number of words in a given block of text:

<?php
   
$summary = <<< summary
   
In the latest installment of the ongoing Developer.com PHP series,
    I discuss the many improvements and additions to
   
<a href="http://www.php.net">PHP 5's</a> object-oriented architecture.
summary;
   
$words = sizeof(explode(' ',strip_tags($summary)));
   
echo "Total words in summary: $words";
?>

This returns the following:

--------------------------------------------
Total words in summary: 22
--------------------------------------------

Theexplode()function will always be considerably faster thanpreg_split(),split(), andspliti(). Therefore, always use it instead of the others when a regular expression isnít necessary.


Note  You might be wondering why the previous code is indented in an inconsistent manner. The multiple-line string was delimited using heredoc syntax, which requires the closing identifier to not be indented even a single space. Why this restriction is in place is somewhat of a mystery, although one would presume it makes the PHP engineís job a tad easier when parsing the multiple-line string. See Chapter 3 for more information about heredoc.


Please check back next week for the next part of this article.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: