Jump to content

match and replace at beginging of each line


skillednerd

Recommended Posts

I am working on parsing a file. I have pretty much getting what I want but I am missing a ' at beginning of line.

How can I put ' at beginning of each line? Thanks in advance

 

example output

 

this', 'that', 'ok', 'now'

 

see just how the beginning of this is missing '

 

Thanks!

Ok no problem.

 

code

 

$lines = file('access.00.log');



foreach ($lines as $line_num => $line) {

$thisx = htmlspecialchars($line);




$patternx = '/\#(.*)\n/i';
$newx = '';

$new_hold = preg_replace($patternx, "", $thisx);


$patterny = '/ /';
$newy = ',';

$new_hold2 = preg_replace("/\s+/", ", '", $new_hold);



$new_hold3 = preg_replace("/,/", "',", $new_hold2);

 

And flash media log I am working with

 

#Version: 1.0
#Start-Date: 2011-04-19 00:02:10
#Software: Adobe Flash Media Server 4.0.0 r1121 x86
#Date: 2011-04-19
#Fields: x-event        x-category      date    time    tz      x-ctx   s-ip    x-pid   x-cpu-load      x-mem-load      x-adaptor       x-vhost x-app   x-appinst       x-duration      x-status        c-ip    c-proto c-proto-ver     s-uri   cs-uri-stem     cs-uri-query    c-referrer      c-user-agent    c-client-id     cs-bytes        sc-bytes        c-connect-type  x-sname x-sname-query   x-suri-query    x-suri-stem     x-suri  x-file-name     x-file-ext      x-file-size     x-file-length   x-spos  c-spos  cs-stream-bytes sc-stream-bytes x-service-name  x-sc-qos-bytes  x-comment       x-eid   x-sid   x-trans-sname   x-trans-sname-query     x-trans-file-ext        x-trans-mode    x-soffset       x-codec-type    x-codec x-plugin        x-page-url      x-smax-rec-size x-smax-rec-duration     x-forwarded-for
app-stop        application     2011-04-19      00:02:10        EDT     45fdkfjwdj      -       12607   -       -       _defaultRoot_   mobile.chatcadet.com    45fdkfjwdj      _definst_       -       -       -       -       -       -       -       -       -       -       -       3073    3376    -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -
app-start       application     2011-04-19      00:02:14        EDT     45fdkfjwdj      -       12607   -       -       _defaultRoot_   mobile.test.com    45fdkfjwdj      _definst_       -       -       -       -       -       -       -       -       -       -       -       0       0       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -
connect-pending session 2011-04-19      00:02:20        EDT     192.168.1.1   192.168.1.1    12607   0       1       _defaultRoot_   mobile.test.com    45fdkfjwdj      _definst_       0       100     76.25.101.105   rtmp    -       rtmp://mobile.test.com/45fdkfjwdj  rtmp://mobile.test.com/45fdkfjwdj  -       rtmp://mobile.test.com/45fdkfjwdj  FMLE/3.0 (compatible; FMSc/1.0) 4702111235045409135     3073    3073    normal  -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -
connect session 2011-04-19      00:02:20        EDT     192.168.1.1   192.168.1.1    12607   0       1       _defaultRoot_   mobile.test.com    45fdkfjwdj      _definst_       0       200     76.25.101.105   rtmp    -       rtmp://mobile.test.com/45fdkfjwdj  rtmp://mobile.testt.com/45fdkfjwdj  -       rtmp://mobile.test.com/45fdkfjwdj  FMLE/3.0 (compatible; FMSc/1.0) 4702111235045409135     3073    3073    normal  -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -
disconnect      session 2011-04-19      00:04:27        EDT     192.168.1.1   192.168.1.1    12607   0       1       _defaultRoot_   mobile.chatcadet.com    45fdkfjwdj      _definst_       127     200     192.168.1.1   rtmp    -       rtmp://mobile.chatcadet.com/45fdkfjwdj  rtmp://mobile.chatcadet.com/45fdkfjwdj  -       rtmp://mobile.test.com/45fdkfjwdj  FMLE/3.0 (compatible; FMSc/1.0) 4702111235045409135     3073    3376    normal  -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -       -

           

Well I want to have the values separated by comma. I thought I needed the 'value' but don't just comma separated.

 

Like

 

app-stop, application, 2011-04-19, 00:02:10   

 

The problem come with the comments in the file with the # before it so it comes at the beginning of file and every so many results there

is another #field list. I do have comma separated working its just the blank lines formed when trying to remove all the lines starting with #

 

The problem is when using:

$patternx = '/^.*\#.*\R$/';

$newx = '';

$new_hold = preg_replace($patternx, $newx, $thisx);

 

It doesn't get rid of the lines it creates a blank line with return in it. So results look like

Blank Line

Blank Line

Blank Line

Blank Line

server-stop, server, 2011-04-19

 

And in the middle of results where field list (# field, field, field .. .) the blank lines are there.

 

And when I use explode to create an array from it messes all that up.

 

 

Thanks I got it to work off your sample code. I finally got it working correctly thanks very much!!

 

$lines = file('access.00.log');
//$opt = array();
foreach($lines as $line){
if(!preg_match("/^#/", $line)){

        $thisx = htmlspecialchars($line);

        $thisx = preg_replace("/\s+/", ", ", $thisx);

       //removed the extra white space and comma on the end
        $thisx = substr_replace($thisx ,"",-2);



        $pC = explode(",", $thisx);

        echo "Value1 = $pC[0]<br />";




}




}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.