Jump to content

read text files with PHP


kney

Recommended Posts

Hey,

 

I'm looking for some PHP code which can read a certain text file i have.

I need to get certain information from this text file.

 

This is how the text file looks like:

 

metadataIngester: now executing performAction with token: 9188147351

metadataIngester: result of performAction with token:9188147351

metadataIngester: runtime (ms) for token 9188147351: 9953

inferencer: now executing performAction with token:9188147351

inferencer: result of performAction with token:9188147351

inferencer: runtime (ms) for token 9188147351: 10903

transcoder: now executing performAction with token:9188147351

transcoder: result of performAction with token:9188147351

transcoder: runtime (ms) for token 9188147351: 65456

transcoder: now executing performAction with token:9188147351

transcoder: result of performAction with token:9188147351

transcoder: runtime (ms) for token 9188147351: 83128

streamingServerTransfer: now executing performAction with token:9188147351

streamingServerTransfer: result of performAction with token:9188147351

streamingServerTransfer: runtime (ms) for token 9188147351: 12187

middleware: now executing performAction with token:9188147351

middleware: result of performAction with token:9188147351

middleware: runtime (ms) for token 9188147351: 28148

 

 

Now, there are more parts like this with an other tokenID & runtimes.

I need to get the runtime for each token (in this example it will be 9953 + 10903 + 65456 + 83128 + 12187 + 28148 = 209775 (ms)).

I need PHP to display the tokenID + the total time per token.

Link to comment
Share on other sites

i would read the text file line by line for starters.

then you would need to use substr() or strpos() to identify where the token is to extract it, proably combined with regular expressions. and do the same for the runtime.

 

although looking at it closely, if its always presented in that format, you could use explode and use colon : as the delimiter, that would also work.

 

as you extract the run times, pass them to an array and use array_sum() to get the final number you are after

Link to comment
Share on other sites

Maybe...

 

<?php

$txtfile = "/path/to/file/name.txt";
$data = fopen($txtfile, "r");
preg_match_all("/runtime (ms) for token (.*?): (.*?)/", $data, $return);

$array[token] = $return['1']['0'];
$array[runtime] = $return['1']['1'];

foreach($array as $var)
{
echo "Token: " . $var['token'] . "<br />Runtime: " . $var['runtime'] . "<br /><br />";
}
fclose($data);

?>

Link to comment
Share on other sites

Didn't feel right not testing and this will work.

 

<?php

$txtfile = "/path/to/file/name.txt";
$handle = fopen($txtfile, "r");
$data = fread($handle, filesize($txtfile));
preg_match_all("/token (.*): (.*?)\n/", $data, $return);

foreach($return['1'] as $key=>$value)
{
echo "Token: " . $return['1'][$key] . "<br />Runtime: " . $return['2'][$key] . "<br /><br />";
}


?>

Link to comment
Share on other sites

Ok, I got this for you. Be warned: not tested, and it only works if your data is formatted exactly as outlined in the sample.

function parseTXT($file){
$fh = fopen($file, "r");
$data = fread($fh,filesize($fh));
fclose($fh);
$d_array = explode("\n", $data);
foreach ($d_array as $val){
	if (strstr($val, "runtime")){
		$tmp_array = explode(" ", $val);
		$token = str_replace(":", "", $tmp_array[5]);
		$token_array[$token][] = $tmp_array[6]; 
	}
}
foreach ($token_array as $key=>$val2){
	$token_array[$key]['total'] = 0;
	foreach ($val2 as $val3){
		$token_array[$key]['total'] += $val3;
	}
}
return $token_array;
}

 

Ok, I tested my code against the sample data you gave, and here's what I got:

Array
(
    [9188147351] => Array
        (
            [0] => 9953
            [1] => 10903
            [2] => 65456
            [3] => 83128
            [4] => 12187
            [5] => 28148
            [total] => 209775
        )

)

look right to you?

Link to comment
Share on other sites

I get a blank page..

But the actual information in text file is a little bit more complicated than the sample I gave.

 

It's like this:

Line 62: 2011-05-02 18:14:17,616 INFO  [com.siemens.vcms.actor.common.JbpmPoller] metadataIngester: now executing performAction with token:9142272199 and context: {contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, contentProviderName=carlo, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client}
Line 91: 2011-05-02 18:14:20,668 INFO  [com.siemens.vcms.actor.common.JbpmPoller] metadataIngester: result of performAction with token:9142272199 : context: {contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingNeeded=yes, contentProviderName=carlo, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client}
Line 125: 2011-05-02 18:14:29,348 INFO  [com.siemens.vcms.actor.common.JbpmPoller] metadataIngester: runtime (ms) for token 9142272199: 13320
Line 178: 2011-05-02 18:14:41,204 INFO  [com.siemens.vcms.actor.common.JbpmPoller] inferencer: now executing performAction with token:9142272199 and context: {inferenceActor=inferencer, contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingNeeded=yes, contentProviderName=carlo, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client}
Line 206: 2011-05-02 18:14:45,065 INFO  [com.siemens.vcms.actor.common.JbpmPoller] inferencer: result of performAction with token:9142272199 : context: {inferenceActor=inferencer, contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, serverLocations=MOVIE:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);PREVIEW:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);POSTER:SD(imageServerIPTV),SD_ENCRYPTED(imageServerIPTV),HD(imageServerIPTV),HD_ENCRYPTED(imageServerIPTV);}
Line 225: 2011-05-02 18:14:49,491 INFO  [com.siemens.vcms.actor.common.JbpmPoller] inferencer: runtime (ms) for token 9142272199: 13634
Line 536: 2011-05-02 18:15:52,550 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: now executing performAction with token:9142272199 and context: {inferenceActor=inferencer, contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingActor=transcoder, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, serverLocations=MOVIE:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);PREVIEW:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);POSTER:SD(imageServerIPTV),SD_ENCRYPTED(imageServerIPTV),HD(imageServerIPTV),HD_ENCRYPTED(imageServerIPTV);}
Line 593: 2011-05-02 18:16:08,035 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: result of performAction with token:9142272199 : context: {inferenceActor=inferencer, contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingActor=transcoder, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, serverLocations=MOVIE:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);PREVIEW:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);POSTER:SD(imageServerIPTV),SD_ENCRYPTED(imageServerIPTV),HD(imageServerIPTV),HD_ENCRYPTED(imageServerIPTV);}
Line 617: 2011-05-02 18:16:12,992 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: runtime (ms) for token 9142272199: 20573
Line 1397: 2011-05-02 18:19:25,685 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: now executing performAction with token:9142272199 and context: {inferenceActor=inferencer, contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingActor=transcoder, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, serverLocations=MOVIE:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);PREVIEW:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);POSTER:SD(imageServerIPTV),SD_ENCRYPTED(imageServerIPTV),HD(imageServerIPTV),HD_ENCRYPTED(imageServerIPTV);}
Line 1398: 2011-05-02 18:19:27,410 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: result of performAction with token:9142272199 : context: {inferenceActor=inferencer, contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingActor=transcoder, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, serverLocations=MOVIE:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);PREVIEW:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);POSTER:SD(imageServerIPTV),SD_ENCRYPTED(imageServerIPTV),HD(imageServerIPTV),HD_ENCRYPTED(imageServerIPTV);}
Line 1425: 2011-05-02 18:19:32,095 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: runtime (ms) for token 9142272199: 6533
Line 4940: 2011-05-02 18:36:11,809 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: now executing performAction with token:9142272199 and context: {inferenceActor=inferencer, contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingActor=transcoder, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, serverLocations=MOVIE:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);PREVIEW:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);POSTER:SD(imageServerIPTV),SD_ENCRYPTED(imageServerIPTV),HD(imageServerIPTV),HD_ENCRYPTED(imageServerIPTV);}
Line 5135: 2011-05-02 18:37:20,676 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: result of performAction with token:9142272199 : context: {inferenceActor=inferencer, contentItemId=21745, workingDir=/content, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingActor=transcoder, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, serverLocations=MOVIE:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);PREVIEW:SD(fileserver),SD_ENCRYPTED(fileserver),HD(fileserver),HD_ENCRYPTED(fileserver);POSTER:SD(imageServerIPTV),SD_ENCRYPTED(imageServerIPTV),HD(imageServerIPTV),HD_ENCRYPTED(imageServerIPTV);}
Line 5151: 2011-05-02 18:37:25,794 INFO  [com.siemens.vcms.actor.common.JbpmPoller] transcoder: runtime (ms) for token 9142272199: 74117
Line 5167: 2011-05-02 18:37:34,153 INFO  [com.siemens.vcms.actor.common.JbpmPoller] streamingServerTransfer: now executing performAction with token:9142272199 and context: {contentItemId=21745, workingDir=/content, transcodingActor=transcoder, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, inferenceActor=inferencer, streamingServerActorTransfer=streamingServerTransfer, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, serverLocations=MOVIE:MTP_INTERNET_MOBILE(hlsServer),MTP_4(hlsServer),MTP_INTERNET_FIXED(hlsServer),MTP_1(3gppServer),SD(fileserver),MTP_3(hlsServer),MTP_2(3gppServer);}
Line 5180: 2011-05-02 18:37:36,605 INFO  [com.siemens.vcms.actor.common.JbpmPoller] streamingServerTransfer: result of performAction with token:9142272199 : context: {contentItemId=21745, workingDir=/content, transcodingActor=transcoder, metaDataIngestActor=metadataIngester, assetDir=/inboxes/carlo, flowcontroller=unauthenticated-ejb-client, inferenceActor=inferencer, streamingServerActorTransfer=streamingServerTransfer, metadataFile=/inboxes/carlo/Carlo0002nl.xml_detected, transcodingNeeded=yes, contentProviderName=carlo, skipApproval=yes, serverLocations=MOVIE:MTP_INTERNET_MOBILE(hlsServer),MTP_4(hlsServer),MTP_INTERNET_FIXED(hlsServer),MTP_1(3gppServer),SD(fileserver),MTP_3(hlsServer),MTP_2(3gppServer);}
Line 5193: 2011-05-02 18:37:41,321 INFO  [com.siemens.vcms.actor.common.JbpmPoller] streamingServerTransfer: runtime (ms) for token 9142272199: 10773

Link to comment
Share on other sites

array key was off. try this:

function parseTXT($data){
$fh = fopen($file, "r");
$data = fread($fh,filesize($fh));
fclose($fh);
foreach ($d_array as $val){
	if (strstr($val, "runtime")){
		$tmp_array = explode(" ", $val);
		$token = str_replace(":", "", $tmp_array[12]);
		$token_array[$token][] = $tmp_array[13];
	}
}
foreach ($token_array as $key=>$val2){
	$token_array[$key]['total'] = 0;
	foreach ($val2 as $val3){
		$token_array[$key]['total'] += $val3;
	}
}
return $token_array;
}
print_r(parseTXT("some_file.txt"));

EDIT to account for double spacing. Darn double spaces where one ain't needed! lol

Link to comment
Share on other sites

A brief explination of my code:

 

It looks for all lines that have the word "runtime" in them. This poses an issue, but not insurmountable.

 

Next, it splits up that line by spaces (only thing that keeps the data separate) into an array.

 

Next, knowing the spacing should always be the same, I can tell which array element will contain the data needed. From there, I throw that into an array with the task number as the key, and the time as children arrays, then does the math to give you total time.

Link to comment
Share on other sites

Yea I looked at ur code and I can understand most of it.

Now, i've got the result i need, thank you for that :)

But it's all like this :P

 

Array ( [9148825717] => Array ( [0] => 7348 [1] => 10715 [2] => 11162 [3] => 6334 [4] => 40840 [5] => 15477 [6] => 37840 [total] => 129716 ) [9142272199] => Array ( [0] => 13320 [1] => 13634 [2] => 20573 [3] => 6533 [4] => 74117 [5] => 10773 [6] => 24740 [total] => 163690 ) [9148825738] => Array ( [0] => 8635 [1] => 8064 [2] => 17312 [3] => 6589 [4] => 80070 [5] => 11008 [6] => 24723 [total] => 156401 ) [9148825756] => Array ( [0] => 11187 [1] => 10547 [2] => 20873 [3] => 7841 [4] => 81832 [5] => 11099 [6] => 24476 [total] => 167855 ) [9148825735] => Array ( [0] => 6791 [1] => 8726 [2] => 23588 [3] => 8028 [4] => 79441 [5] => 10825 [6] => 24768 [total] => 162167 ) [9148825789] => Array ( [0] => 8455 [1] => 10828 [2] => 24139 [3] => 8083 [4] => 80254 [5] => 10801 [6] => 25288 [total] => 167848 ) [9148825799] => Array ( [0] => 6813 [1] => 10330 [2] => 44947 [3] => 78526 [4] => 10648 [5] => 24172 [total] => 175436 ) [9148825792] => Array ( [0] => 7579 [1] => 16432 [2] => 42389 [3] => 77346 [4] => 15752 [5] => 26582 [total] => 186080 ) [9155379329] => Array ( [0] => 8529 [1] => 13763 [2] => 44224 [3] => 86303 [4] => 9458 [5] => 24558 [total] => 186835 ) [9155379374] => Array ( [0] => 8813 [1] => 11437 [2] => 45023 [3] => 75839 [4] => 10805 [5] => 26101 [total] => 178018 ) [9155379371] => Array ( [0] => 8446 [1] => 13491 [2] => 54721 [3] => 76985 [4] => 9001 [5] => 22987 [total] => 185631 ) [9161932943] => Array ( [0] => 14106 [1] => 16542 [2] => 64032 [3] => 4219 [4] => 4679 [5] => 4856 [6] => 4704 [7] => 5187 [8] => 4900 [9] => 4920 [10] => 5043 [11] => 5125 [12] => 5283 [13] => 5205 [14] => 5328 [15] => 5361 [16] => 7393 [17] => 5605 [18] => 5631 [19] => 5526 [20] => 5720 [21] => 5640 [22] => 7109 [23] => 5858 [24] => 5917 [25] => 5954 [26] => 6067 [27] => 6165 [28] => 6176 [29] => 5528 [30] => 5321 [31] => 5386 [32] => 5464 [33] => 5550 [34] => 5614 [35] => 5721 [36] => 5748 [37] => 6992 [38] => 5965 [39] => 5826 [40] => 6044 [41] => 5907 [total] => 313317 ) [9155379395] => Array ( [0] => 11560 [1] => 12468 [2] => 65895 [3] => 4172 [4] => 65332 [total] => 159427 ) [9155379377] => Array ( [0] => 9915 [1] => 13393 [2] => 68623 [3] => 4347 [4] => 65072 [total] => 161350 ) [9168486522] => Array ( [0] => 8580 [1] => 11040 [2] => 79796 [3] => 3941 [4] => 66310 [total] => 169667 ) [9168486504] => Array ( [0] => 11491 [1] => 8661 [2] => 84017 [3] => 3982 [4] => 65273 [total] => 173424 ) [9161932983] => Array ( [0] => 9978 [1] => 10500 [2] => 79979 [3] => 5649 [4] => 68438 [total] => 174544 ) [9161932986] => Array ( [0] => 10859 [1] => 9407 [2] => 67504 [3] => 3809 [4] => 65139 [total] => 156718 ) [9168486580] => Array ( [0] => 10062 [1] => 8860 [2] => 75866 [3] => 3951 [4] => 65420 [total] => 164159 )

 

so there are no breaks ^^

Link to comment
Share on other sites

try this:

foreach (parseTXT("some_file.txt") as $key=>$val){
echo "<h2>Statistics for $key: </h2><br />";
echo "<table border=\"0\"><tr><th>MS to Complete</th></tr>";
foreach ($val as $key2=>$val2){
	if (is_int($key2)){
		echo "<tr><td>$val2</td></tr>";
	}
	else{
		echo "</table><br /> <h2><strong>Total Run on $key: $val2</strong</h2><br /><br /><br />";
	}
}
}

Link to comment
Share on other sites

Sorry to bother you guys again with this.

But I'm also looking for an average for the steps in between, listed below. (so for the smaller ones who are forming the total)

Now there are 5 steps we get times for:

metadataIngester
inferencer
transcoder
streamingServerTransfer
middleware

 

But it could occur, some step will have more times, but only the first time the result should be taken and the next time on the same step should be skipped.

Link to comment
Share on other sites

Sorry to bother you guys again with this.

But I'm also looking for an average for the steps in between, listed below. (so for the smaller ones who are forming the total)

Now there are 5 steps we get times for:

metadataIngester
inferencer
transcoder
streamingServerTransfer
middleware

 

But it could occur, some step will have more times, but only the first time the result should be taken and the next time on the same step should be skipped.

 

The average per step isn't really necessary but the double times per step need to be skipped somehow.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.