/    Sign up×
Community /Pin to ProfileBookmark

parsing a text file – explode and unserialize?

Hi,

I have a log file containing a list of serialized data, as shown here:

[url]http://designerdeveloper.net/log.txt[/url]

The format is:

FILEID|||serialize($data)
FILEID2|||serialize($data2)

I need to decode this data into the following:

Field 1 : Value
Field 2 : Value
etc.

I am not sure on how to do this – I think I should first explode the data with the separator “|||”, and then unserialize it, but I am not sure how.

Any help would be greatly appreciated.

[upl-file uuid=0be88b27-bdb7-48c6-90b8-024016d856d0 size=3kB]log.txt[/upl-file]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bokehMay 30.2006 — [code=php]<?php

$file = 'http://designerdeveloper.net/log.txt';
$output = array();

foreach(file($file) as $line)
{
list($k, $v) = explode('|||', trim($line));
$output[$k] = unserialize($v);
}

# test it
echo'<pre>'; var_dump($output);echo'</pre>';
?>[/code]
Copy linkTweet thisAlerts:
@2dazedauthorMay 30.2006 — thanks, got it sorted!
Copy linkTweet thisAlerts:
@2dazedauthorMay 31.2006 — hmm, i've got stuck again :mad: would really appreciate any help!

My second task is that I'm trying to replace the field numbers (e.g [1] , [2] , [3]) with my own field names (e.g. name, time, address)

here is the code I've written so far:
[CODE]
<?php
session_start();
if ($_POST['txtUserId'] === 'admin' && $_POST['txtPassword'] === 'admin') {

$_SESSION['basic_is_logged_in'] = true;}
else
{$_SESSION['basic_is_logged_in'] = false;}



// is the one accessing this page logged in or not?
if (!isset($_SESSION['basic_is_logged_in'])
|| $_SESSION['basic_is_logged_in'] !== true) {

// not logged in, move to login
?>

<html>
<head>

<title></title>



</head>

<body>
<form method="post" name="frmLogin" id="frmLogin" action="testexplode2.php">
<table width="400" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td width="150">User Id</td>
<td><input name="txtUserId" type="text" id="txtUserId"></td>
</tr>
<tr>
<td width="150">Password</td>
<td><input name="txtPassword" type="password" id="txtPassword"></td>
</tr>
<tr>
<td width="150">&nbsp;</td>
<td><input type="submit" name="btnLogin" value="Login"></td>
</tr>
</table>
</form>


</body>
</html>





<?php
exit;
}

?>

<html>
<head>

<title></title>



</head>

<body>


<?php


$file = file('log.txt');
array_multisort($file, SORT_DESC);
foreach ($file as $line)
{

$lines = explode("|||", $line);

print $lines[0];
print '<pre>';
print_r( unserialize( $lines[1] ));
print '</pre>';
}

?>

</body>
</html>
[/CODE]


please help!

You can see how this code works at the moment at [URL=http://designerdeveloper.net/vincent/testexplode2.php]http://designerdeveloper.net/vincent/testexplode2.php[/URL]
Copy linkTweet thisAlerts:
@bokehMay 31.2006 — One possible method is to define constants (with human readable names) and assign them with numeric values that correspond with your array keys. [code=php]<?php

$file = 'http://designerdeveloper.net/log.txt';
$output = array();

foreach(file($file) as $line)
{
list($k, $v) = explode('|||', trim($line));
$output[$k] = unserialize($v);
}

define('first_name', 1);

foreach($output as $sub_array)
{
echo '<p>'.$sub_array[first_name].'</p>';
}
?>[/code]
×

Success!

Help @2dazed spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...