Page 1 of 1

Parsing Text substr

Posted: Thu May 12, 2005 11:43 pm
by aybra
Hello guys, i've been beating my head on the wall over this one...

Code: Select all

//rawData these are the formats its entered into the DB.. i've changed some of it in a third party editor...
(1115944999)[Thu May 12 18:43:19 2005] You tell george,"hiyas"
//the text I edited in Excel, then uploaded to DB.
1115347805 "You tell george,""hehe"""
This is my DB

Code: Select all

CREATE TABLE IF NOT EXISTS `foo` (
  `rawData` longtext NOT NULL,
  `ID` int(6) unsigned zerofill NOT NULL auto_increment,
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
What i want to do
1. Search and pull out the first format
2. change it to the second format
3. take them both, and insert them back into a second DB that will have a third field. (date)

Code: Select all

CREATE TABLE IF NOT EXISTS `foo` (
  `rawData` longtext NOT NULL,
  `date` varChar(10) NOT NULL,
  `ID` int(6) unsigned zerofill NOT NULL auto_increment,
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

   if(substr($rawData, 0) != "(") {
           $rawData = trim(substr($rawData, 10, -1));
           $date = rtrim(substr($rawData, 0, 9));
//date1 is only for reporting
           $date1 = date('M-D-y', $date);
           } else {
           $rawData = $row["rawData"];
           $date = rtrim(substr($rawData, 1, 10));
//date1 is only for reporting
   $date1 = date('M-D-y', $date);
 }
(DB has 500000+ lines in it)
However the above code refuses to report the correct data, so I have yet to try it on the data base yet.

does any one know how to do what i'm trying to do?

Any help would be really appreciated!!!

(fyi this is for an EQ2 log parser)

Re: Parsing Text substr

Posted: Fri May 13, 2005 4:47 am
by timvw
aybra wrote:Hello guys, i've been beating my head on the wall over this one...

Code: Select all

//rawData these are the formats its entered into the DB.. i've changed some of it in a third party editor...
(1115944999)[Thu May 12 18:43:19 2005] You tell george,"hiyas"
//the text I edited in Excel, then uploaded to DB.
1115347805 "You tell george,""hehe"""
The easiest options (imho) are:

http://www.php.net/sscanf

Code: Select all

list($number, $date, $msg) = sscanf($line, "(%d) [%s] %s");
echo $number; echo $date; echo $msg;

http://www.php.net/preg_match

Code: Select all

preg_match("#\((\d+)\) \[(.*?)\] (.*?)#", $line, $matches);
print_r($matches);

Posted: Fri May 13, 2005 7:21 am
by aybra
Thanks I'll go try that out now

Posted: Fri May 13, 2005 10:50 am
by aybra
I gave that a whirl, and I could get the top snippet to work, but the bottom snippit pushes out a blank Array. If i tried to read out the array()

I would get the letter in that spot

i.e.

Code: Select all

// when parsing this text with the second snippit 1115347805 "You tell george,""hehe"""
$foo = $matches["3"];

resulted in:

"1"
I'm not in front of my code right now (i'm sitting at work) so I cannot remember exactly how wrong i'm doing it.

if there is any thing you can deduce from what i've said please let me know, or else i'll post my script when I get home.!!

Posted: Fri May 13, 2005 11:38 am
by timvw
well, both snippets were supposed to do the same... so if one of them works, you should be fine already :) (read: to lazy to look up where i made a mistake)

all you have to do now is read the documentation on sscanf, get inspired, checkout the relevant links mentionned in the docs.. and discover sprintf to generate the new string :)

Posted: Fri May 13, 2005 11:50 am
by aybra
Thanks I'll look into that!!

Let ya know when I get home if it works.

Posted: Sat May 14, 2005 5:53 pm
by aybra
Ok.. i've got it mostly working here.

Code: Select all

// when i do
             $line = "(1116099225)[Sat May 14 13:33:45 2005] It will take about 5 more seconds to prepare your camp. ";
$p = "<p>";
       list($number, $date, $msg) = sscanf($line, "(%d) [%s] %s");
echo '',$number,'',$p,'';
echo '',$date,'',$p,'';
echo '',$msg,'',$p,'';

// I get

/*
1116099177

Sat
*/

// $number reports right, $date reports empty, and $msg reports Sat.. wich makes no sence to me.

what does %s do any how? i've tried to find it on php.net, and i searched google for %s, but it gives me "s" results.. wich is allot.

any one know? or can help?

Posted: Sat May 14, 2005 5:56 pm
by Sphen001
aybra wrote:what does %s do any how? i've tried to find it on php.net, and i searched google for %s, but it gives me "s" results.. wich is allot.
It tells PHP how to format certain strings. Try looking up the functions printf(), print_r(), and fprint_r(). It should give you what you need.

Sphen001

EDIT:

It's under the one for sprintf()