Page 2 of 2

Re: Help parsing a large group of text for certain values?

Posted: Thu Dec 02, 2010 3:29 pm
by McInfo
Sorry to interrupt. I wrote a function because I think this problem is general enough that the solution could be useful to many people. The function is a little fragile (meaning non-standard input results in weird output), but I'll just release it now because I don't think I will have a chance to make it more robust in the near future. It has not been thoroughly tested.

Code: Select all

<?php
/**
 * Splits a string on multiple, sequential delimiters. Works like explode(),
 * but with an array of delimiters.
 *
 * @param array $delimiters
 * @param string $subject
 * @return array of values
 */
function fragmentize ($delimiters, $subject) {
    $values = array();
    $cpos = 0;
    $count = count($delimiters);
    for ($i = 0; $i < $count; ++$i) {
        $curr = $delimiters[$i];
        $cpos = strpos($subject, $curr, $cpos) + strlen($curr);
        $next = ($i < $count - 1) ? $delimiters[$i + 1] : null;
        $npos = ($next) ? strpos($subject, $next, $cpos) : strlen($subject);
        $values[$i] = trim(substr($subject, $cpos, $npos - $cpos));
    }
    return $values;
}

//------------------------------------------------------------------------------
// Usage:

$string = 'Help Number: 125086 ENTERED ON 11/29/10 AT 14:33 BY JOHN SMITH CURRE'
    . 'NT ESTIMATED COMPLETION IS 12/06/10 AT 15:00 TYPE OF PROBLEM: T.P. BIG I'
    . 'SSUE PRIORITY: 5 WORKING DAYS ITEM SUBMITTED BEFORE? NO ------------ PRO'
    . 'BLEM DESCRIPTION ------------ SOME LONG DESCRIPTION';
echo wordwrap($string, 80), "\n";

$delimiters = array (
    'Help Number:',
    'ENTERED ON',
    'AT',
    'BY',
    'CURRENT ESTIMATED COMPLETION IS',
    'AT',
    'TYPE OF PROBLEM:',
    'PRIORITY:',
    'ITEM SUBMITTED BEFORE?',
    '------------ PROBLEM DESCRIPTION ------------'
);
print_r($delimiters);

$values = fragmentize($delimiters, $string);
print_r($values);
/*
Array
(
    [0] => 125086
    [1] => 11/29/10
    [2] => 14:33
    [3] => JOHN SMITH
    [4] => 12/06/10
    [5] => 15:00
    [6] => T.P. BIG ISSUE
    [7] => 5 WORKING DAYS
    [8] => NO
    [9] => SOME LONG DESCRIPTION
)
*/

Re: Help parsing a large group of text for certain values?

Posted: Thu Dec 02, 2010 4:31 pm
by Jonah Bron
Sweet, bookmarked :)

Re: Help parsing a large group of text for certain values?

Posted: Fri Dec 03, 2010 10:48 am
by Insyderznf
Nice function, that really helps!