I'm working on a script that will parse through a long string using
regexs to pattern match a certain format. I'm having an issue with a
'?>' in a string being picked up as an end-of-code character, but only
if the line before it is commented out. If the line before is NOT
commented out, PHP processes the file as normal (errors out, but I can
fix that). I'd appreciate it if someone else could run this script
and see what happens.
PHP Version:
$ php --version
PHP 4.3.10 (cli) (built: Dec 20 2004 10:45:58)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
PHP Config line:
./configure --with-mysql --with-apxs2=/pub/apache/bin/apxs
--with-mcrypt=/usr/local/lib --with-curl=/usr/local --enable-ftp
--with-imap=/usr/local/imap --with-jpeg --with-jpeg-dir=/usr/local/lib
--with-png --with-png-dir=/usr/local/lib
--with-zlib-dir=/usr/local/lib --with-gd --with-freetype
--with-freetype-dir=/usr/local/lib --with-ttf
Unexpected results:
If line 16 (indicated below) is commented out, the '?>' in the string
on line 17 makes PHP stop parsing and the rest of the script is simply
dumped to stdout. If line 16 is NOT commented out, the '?>' is NOT
picked up as being a PHP tag and it parses the script as PHP.
Environment:
Running in CLI on Redhat 9, current versions of all applicable
software. The exact same behavior occurs when running as a webpage
with Apache 2.0.52.
==========
<?
$notes = '14 Mar 2004 18:46 > Jay Brown added item to shop14 Mar 2004
19:15> Jay Brown changed production status to Ready for Publishing14
Mar 2004 19:32 > Published by Jay Brown15 Mar 2004 09:22 > Published
by bill Smith15 Mar 2004 09:57> Published by Bill Smith16 Jun 2004
14:28> Julie Johnson repriced all options.
16 Jun 2004 14:29> Julie Johnson repriced all options.6/16/2004 14:29>
Julie Johnson committed pricing changes.
29 Jun 2004 09:21> Published by Bill Smith07 Jul 2004 11:42> Bill
Smith changed production status to Ready for Publishing
07 Jul 2004 13:44> Published by Bill Smith20 Jul 2004 13:07> Bonie
Jackson changed production status to Ready for Publishing
21 Jul 2004 08:36> Published by Bill Smith';
# Attempt to split the notes....
# echo $notes."\n";
$time_exp = '/\d{1,2} \w{3} \d{4} \d{2}:\d{2}/';
$note_exp = '/\d{1,2} \w{3} \d{4} \d{2}:\d{2}\s?> [!-~ ]+/';
// COMMENT OUT THIS LINE
$note_exp = '/\d{1,2}\/\d{1,2}\/\d{2,4} \d{2}:\d{2}\s?>
[!-~]+/'; // PHP STOPS PARSING HERE
// Attempt to split the notes...
if (preg_match($note_exp, $notes) == 0)
{
echo "No match found for ".$record['ID']."!:\n";
echo $notes."\n\n\n";
continue;
}
$count = preg_match_all ($note_exp, $notes, $splits);
$notes = $splits[0];
// Check for "notes within notes"... This can happen when
there are a missing line break in a note...
$temp = array();
foreach ($notes as $note)
{
while (preg_match($note_exp, $note) > 0)
{
preg_match_all($note_exp, $note, $splits,
PREG_OFFSET_CAPTURE, 5); // Find the next substring match...
if (isset ($splits[0][0]))
{
$temp[] = substr($note, 0, $splits[0][0][1]);
$note = $splits[0][0][0];
}
else break;
}
$temp[] = $note;
}
$notes = $temp;
// Remove any duplicate notes....
$unique_notes = array();
foreach ($notes as $note)
{
if (!in_array($note, $unique_notes))
$unique_notes[] = $note;
}
# Split the note into different fields for storing in the DB...
$item_notes = array(); $count = 1;
foreach ($unique_notes as $note)
{
// Find the timestamp...
preg_match_all ($time_exp, $note, $splits);
$timestamp = $splits[0][0];
// Convert it to a legitimate timestamp...
$ansi_timestamp = date('Y-m-d H:i:s', strtotime($timestamp));
// Find the note itself...
$note_divider = strpos($note, '>');
$note_only = trim(substr($note, $note_divider+1));
$a = array();
$a['RecordID'] = $record['Target'];
$a['Timestamp'] = $ansi_timestamp;
$a['Log'] = addslashes($note_only);
$all_notes[] = $a;
$count++;
}
foreach ($all_notes as $data)
{
echo '{ Timestamp="'.$data['Timestamp'].'", RecordID =
"'.$data['RecordID'].'", Log="'.$data['Log'].'" }';
}
?>
==========
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php