Home arrow PHP arrow Parsing Strings and Regular Expressions

Parsing Strings and Regular Expressions

In this fourth part of a five-part series on strings and regular expressions in PHP, you'll learn how to perform complex string parsing, find the last occurrence of a string, 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. Parsing Strings and Regular Expressions
  2. Finding the Last Occurrence of a String
  3. Retrieving Part of a String
  4. Determining the Frequency of a String’s Appearance
By: Apress Publishing
Rating: starstarstarstarstar / 1
July 08, 2010

print this article



Converting an Array into a String

Just as you can use theexplode()function to divide a delimited string into various array elements, you concatenate array elements to form a single delimited string using theimplode()function. Its prototype follows:

string implode(string delimiter, array pieces)

This example forms a string out of the elements of an array:

    $cities = array("Columbus", "Akron", "Cleveland", "Cincinnati");
    echo implode("|", $cities);

This returns the following:


Performing Complex String Parsing

Thestrpos()function finds the position of the first case-sensitive occurrence ofsubstrin a string. Its prototype follows:

int strpos(string str, string substr [, int offset])

The optional input parameteroffsetspecifies the position at which to begin the search. Ifsubstris not instr,strpos()will returnFALSE. The optional parameteroffsetdetermines the position from whichstrpos()will begin searching. The following example determines the timestamp of the first timeindex.htmlis accessed:

    $substr = "index.html";
    $log = <<< logfile[2006/02/10:20:36:50][2006/02/11:04:15:23][2006/02/15:17:25]
    // What is first occurrence of the time $substr in log?
    $pos = strpos($log, $substr);

    // Find the numerical position of the end of the line
    $pos2 = strpos($log,"\n",$pos);

    // Calculate the beginning of the timestamp
$pos = $pos + strlen($substr) + 1;

    // Retrieve the timestamp
    $timestamp = substr($log,$pos,$pos2-$pos);

    echo "The file $substr was first accessed on: $timestamp";

This returns the position in which the fileindex.htmlis first accessed:

The file index.html was first accessed on: [2006/02/10:20:36:50]


The functionstripos()operates identically tostrpos(), except that it executes its search case insensitively.

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