Jump to content

Complicated file parsing


BlackShark

Recommended Posts

Hello all,

 

I have an file with this structure attached.

The question is how can that file be parsed line by line so I can manipulate further the info as following:

1. I want to know the >LD XX. In every LD there could be several prints

2. Skip the lines describing memory etc.. between LD xx and REQ

3. Get variables in an array form as (variable) REQ -> (value) PRT

4. Skip the empty lines

 

Thank you

SPN.txt

Link to comment
https://forums.phpfreaks.com/topic/277248-complicated-file-parsing/
Share on other sites

What have you tried? Where are you stuck?

 

We need to see code.

Here is the code that I have so far:

<?php
$txt_file    = file_get_contents('SPN.txt');
$rows        = explode("\n", $txt_file);
array_shift($rows);

foreach($rows as $row => $data)
{
    //get row data
    $row_data = explode('^', $data);
    $info[$row]['id'] = $row_data[0];
    //display data
    echo 'Row ' . $row . ': ' . $info[$row]['id'] . '<br />';

    //list variables
    $variable = explode(' ', $info[$row]['id']);

foreach($variable as $row_var) {
	echo ' - ' . $row_var . '<br />';
}	

    echo '<br />';
}
?>

The problem now is that it takes space as an variable. For example from CUST 0 I should have 2 variables: CUST and 0. Now I have CUST, (space) and 0. 

Any ideas?

Actually I think that I'm complicating the issue with this direction.

What I need is to find for every variable it's value: SPN  0(for spn value 0), INPL YES(for inpl value yes). As in my first direction I was trying to find the variable and value using space explode but now I see that not all the lines have 1 space between variable and value.

Added preg split and resolved the above issue

<?php
$txt_file    = file_get_contents('SPN.txt');
$rows        = explode("\n", $txt_file);
array_shift($rows);

foreach($rows as $row => $data)
{
    //get row data
    $row_data = explode('^', $data);
    $info[$row]['variable'] = $row_data[0];
    $info[$row]['value'] = $row_data[2];

//echo $info[$row]['value'];
    
    //display data
    echo 'Row ' . $row . ': ' . $info[$row]['variable'] . '<br />';

    //list variables
    $variable = explode(' ', $info[$row]['variable']);
$result = preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $info[$row]['variable'], -1, PREG_SPLIT_NO_EMPTY);
foreach($result as $row_var) {

echo ' - ' .$row_var. '<br />';
}	
   echo '<br />';
}
?>

I have one more issue regarding my project. I want to group my info from bellow, info that it repeats successively:

ID	Variable	Value
1	CUST	0
3	FEAT	net
5	TRAN	AC1
7	TYPE	SPN
11	SPN	0
12	FLEN	0
13	INPL	YES
14	RLI	9
15	SDRR	NONE
16	ITEI	NONE
18	SPN	1
19	FLEN	0
20	RLI	9
21	SDRR	NONE
22	ITEI	NONE
24	SPN	2
25	FLEN	0
26	RLI	9
27	SDRR	NONE
28	ITEI	NONE

The desired layout should be as in the picture bellow:

 

layout.jpg

 

Also this is the code that I have now:

<?php
$txt_file    = file_get_contents('SPN.txt');
$rows        = explode("\n", $txt_file);
array_shift($rows);
?>
<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="30%" cellpadding="0" cellspacing="0">
	<tr>
		<td>ID</td>
		<td>Variable</td>
		<td>Value</td>
	</tr>	
<?php
foreach($rows as $row => $data)
{
    //get row data
	$row_data = explode('^', $data);
	$result0 = preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $data, -1, PREG_SPLIT_NO_EMPTY);
if(!empty($result0)){ 
    //display data
?>
	<tr>
		<td><?php echo $row; ?></td>
		<td><?php echo $result0[0]; ?></td>
		<td><?php echo $result0[1]; ?></td>
	</tr>
<?php }} ?>
</table>

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.