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
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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />';
}
?>
Link to comment
Share on other sites

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