skillednerd Posted April 19, 2011 Share Posted April 19, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/234187-match-and-replace-at-beginging-of-each-line/ Share on other sites More sharing options...
Pikachu2000 Posted April 19, 2011 Share Posted April 19, 2011 It would be easier to answer if you'd post the code, and some sample data. Quote Link to comment https://forums.phpfreaks.com/topic/234187-match-and-replace-at-beginging-of-each-line/#findComment-1203673 Share on other sites More sharing options...
skillednerd Posted April 19, 2011 Author Share Posted April 19, 2011 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Quote Link to comment https://forums.phpfreaks.com/topic/234187-match-and-replace-at-beginging-of-each-line/#findComment-1203691 Share on other sites More sharing options...
The Little Guy Posted April 20, 2011 Share Posted April 20, 2011 What do you want from that file (example?)? Quote Link to comment https://forums.phpfreaks.com/topic/234187-match-and-replace-at-beginging-of-each-line/#findComment-1203807 Share on other sites More sharing options...
skillednerd Posted April 20, 2011 Author Share Posted April 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234187-match-and-replace-at-beginging-of-each-line/#findComment-1203825 Share on other sites More sharing options...
The Little Guy Posted April 20, 2011 Share Posted April 20, 2011 Maybe this will get you started: <?php $lines = file('access.00.log'); $opt = array(); foreach($lines as $line){ if(!preg_match("/^#/", $line)){ $opt[] = preg_split("/\s\s+/", $line); } } print_r($opt); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234187-match-and-replace-at-beginging-of-each-line/#findComment-1203832 Share on other sites More sharing options...
skillednerd Posted April 20, 2011 Author Share Posted April 20, 2011 That works but I can't get the lines out of the array though. I am trying $opt[0] and can't actually pull the lines from it Quote Link to comment https://forums.phpfreaks.com/topic/234187-match-and-replace-at-beginging-of-each-line/#findComment-1203839 Share on other sites More sharing options...
skillednerd Posted April 20, 2011 Author Share Posted April 20, 2011 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 />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/234187-match-and-replace-at-beginging-of-each-line/#findComment-1203843 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.