/preg_magic.php

Description

preg_magic

Class/Function to assist with parsing great piles of garbage (ie: HTML) into nice hashes/associative arrays with a few regular expressions.

License

 This library is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see .

Classes
Class Description
preg_magic preg_magic
Functions
preg_magic (line 73)

preg_magic

Alias for preg_magic::execute()

Basic synapsis:

preg_magic($Fields, $Target)

$Fields is an associative array that contains the structure you expect to extract from the file. It is very important to understand that you must supply the fields in the order that you expect them to be matched.

There are currently two types of fields

  • simple
  • table
A simple field is simply defined as 'name' => 'expression to match'

A table field is a whole lot more complicated but it's also very flexable, and is defined as follows

    '@table'       => true,
    '@table_start' => 'the expression to match at the start of the table',
    '@table_end'   => 'the expression to match at the end of the table',
    '@table_fields => array(of more fields),

Both @table_start and @table_end will be ignored if this is the root element of the array

So an example structure might look like

  1. <?php
  2. $HistoryFields = array(
  3.     'cardnumber'    => 'card number:<\/span>\s*((?:\d{4}\s*){3}\d{4})',
  4.     'balance'       => 'Card balance:<\/td>\s*<td>\s*(\$\d+\.\d+)',
  5.     'history'       => array(
  6.         '@table'        => true,
  7.         '@table_start'  => 'class="results_table">.+?<\/tr>\s*',
  8.         '@table_end'    => '\s*<\/table>',
  9.         '@table_fields' => array(
  10.             'stamp'        => '\s*<tr class="(?:odd|even)">\s*'
  11.                               . '<td>(\d+-\w+-\d+\s*\d+:\d+\s*\w+)<\/td>',
  12.             'type'         => '<td.*?>\s*([^<]*?)\s*<\/td>',
  13.             'location'     => '<td.*?>\s*([^<]*?)\s*<\/td>',
  14.             'amount'       => '<td.*?>\s*([^<]*?)\s*<\/td>',
  15.             'gst'          => '<td.*?>\s*([^<]*?)\s*<\/td>\s*<\/tr>\s*'
  16.         )
  17.     )
  18. );
This example shows two simple fields, and a table

The structure can be recursive, ie, you can have more table and simple fields beneith the table field

A full example might look like this

  1. <?php
  2. include_once('../preg_magic.php');
  3.  
  4. $HistoryFields = array(
  5.     'cardnumber'    => 'card number:<\/span>\s*((?:\d{4}\s*){3}\d{4})',
  6.     'balance'       => 'Card balance:<\/td>\s*<td>\s*(\$\d+\.\d+)',
  7.     'history'       => array(
  8.         '@table'        => true,
  9.         '@table_start'  => 'class="results_table">.+?<\/tr>\s*',
  10.         '@table_end'    => '\s*<\/table>',
  11.         '@table_fields' => array(
  12.             'stamp'        => '\s*<tr class="(?:odd|even)">\s*'
  13.                               . '<td>(\d+-\w+-\d+\s*\d+:\d+\s*\w+)<\/td>',
  14.             'type'         => '<td.*?>\s*([^<]*?)\s*<\/td>',
  15.             'location'     => '<td.*?>\s*([^<]*?)\s*<\/td>',
  16.             'amount'       => '<td.*?>\s*([^<]*?)\s*<\/td>',
  17.             'gst'          => '<td.*?>\s*([^<]*?)\s*<\/td>\s*<\/tr>\s*'
  18.         )
  19.     )
  20. );
  21.  
  22. $HistoryHTML = preg_replace("/\s\s+|&nbsp;/",' ',file_get_contents('history.html'));
  23.  
  24. $Output = preg_magic::execute($HistoryFields, $HistoryHTML);
  25.  
  26. print_r($Output);

  • return: An associative array containing data, or false on failure to match
array|false preg_magic (array $Fields, string $Target)
  • array $Fields: An associative array of fields to be use
  • string $Target: The target string to process

Documentation generated on Tue, 05 Feb 2008 22:29:20 +1100 by phpDocumentor 1.4.1