What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP parsing certain lines of a text file

Pages: (2): « First [ 1 ] 2 » Last »
PHP parsing certain lines of a text file
Author: Message:
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
28 / Male / Flag
Joined: Jul 2006
O.P. PHP parsing certain lines of a text file
Hey there,

I was wondering if any of you knows how to parse a text file similar to this:

quote:
"Advertisements"
{
    "1"
    {
        "type"        "S"
        "text"        "Ad1"
    }
    "2"
    {
        "type"        "S"
        "text"        "Ad2"
    }
    "3"
    {
        "type"        "S"
        "text"        "Ad3"
    }
}

But only parse the ad number and the section after the word "text". For example, in the above document, it would parse:
quote:
1) Ad1
2) Ad2
3) Ad3

Thanks
11-22-2009 07:13 PM
Profile E-Mail PM Find Quote Report
John Anderton
Elite Member
*****

Avatar

Posts: 3908
Reputation: 80
34 / Male / Flag
Joined: Nov 2004
Status: Away
RE: PHP parsing certain lines of a text file
Use regexp and read up on your preg_match_all ;)

Should be simple enough to do :)
[

KarunAB.com
]

[img]http://gamercards.exophase.com/459422.png[
/img]
11-22-2009 07:22 PM
Profile E-Mail PM Web Find Quote Report
prashker
Veteran Member
*****


Posts: 5107
Reputation: 104
– / Male / –
Joined: Mar 2005
Status: Away
RE: PHP parsing certain lines of a text file
This would be so much easier if the system making the text file used json_encode, and decrypted it with json_decode

json_encoded arrays look very similar to the text you want parsed, if you cannot change the initial text file and how it's generated....follow JAndertons advice :p

This post was edited on 11-22-2009 at 08:00 PM by prashker.
11-22-2009 07:59 PM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
28 / Male / Flag
Joined: Jul 2006
O.P. RE: PHP parsing certain lines of a text file
Thanks, and yeah, I cannot change the original text file as its read by something else, that can only read in that format. Anway, I managed to come up with this:

PHP code:
 
<?php
$text = file_get_contents('advertisements.txt');
preg_match_all('~"text"\s+"([^"]*)~', $text, $matches);
$arr = str_replace('\n', "<br>", $matches[1]);
echo ("<li>");
echo implode("<br /><li>\n", $arr);
?>
 


Yes, pretty fail way of doing it, especially the <li> part, but hey, it works :)

This post was edited on 11-23-2009 at 12:24 AM by Jimbo.
11-23-2009 12:07 AM
Profile E-Mail PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
29 / Male / Flag
Joined: Jul 2006
RE: PHP parsing certain lines of a text file
if you want you can just do it like this:

code:
<?php

$c = file_get_contents('./ads.txt');
preg_match_all('/\s*"text"\s*"([^"]*)"/',$c,$m);
echo "<ol>";
for($i=0;$i<count($m[1]);$i++){
    echo "<li>".$m[1][$i]."</li>";
}
echo "</ol>";

?>

its a better way of doing the list, and maybe a little safer with the finding of the text [=
11-23-2009 12:36 AM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
28 / Male / Flag
Joined: Jul 2006
O.P. RE: PHP parsing certain lines of a text file
Thanks NanaFreak :)

Do you know how I would go about changing the colour/font of each Ad, so for example, make Ad1 blue, Ad2, white, Ad3, black?
11-23-2009 11:45 PM
Profile E-Mail PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
29 / Male / Flag
Joined: Jul 2006
RE: PHP parsing certain lines of a text file
you are either able to hard code that information, but if you want to make it dynamic, you can add another element to the text document...

method 1:

code:
<?php

$a = array('blue','white','black');

$c = file_get_contents('./ads.txt');
preg_match_all('/\s*"text"\s*"([^"]*)"/',$c,$m);
echo "<ol>";
for($i=0;$i<count($m[1]);$i++){
    echo "<li><span style='color:".$a[$i]."'>".$m[1][$i]."</span></li>";
}
echo "</ol>";

?>

method 2:

text document:

code:
"Advertisements"
{
    "1"
    {
        "type"        "S"
        "text"        "Ad1"
        "color"       "blue"
    }
    "2"
    {
        "type"        "S"
        "text"        "Ad2"
        "color"       "white"
    }
    "3"
    {
        "type"        "S"
        "text"        "Ad3"
        "color"       "black"
    }
}

php file:

code:
<?php

$c = file_get_contents('./ads.txt');
preg_match_all('/\s*"text"\s*"([^"]*)"/',$c,$m);
preg_match_all('/\s*"color"\s*"([^"]*)"/',$c,$a);
echo "<ol>";
for($i=0;$i<count($m[1]);$i++){
    echo "<li><span style='color:".$a[1][$i]."'>".$m[1][$i]."</span></li>";
}
echo "</ol>";

?>


hope this help [=
11-24-2009 12:32 AM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
28 / Male / Flag
Joined: Jul 2006
O.P. RE: PHP parsing certain lines of a text file
Thanks again nanafreak :)

As you seem to be a php genius, I have something else you may be able to solve. ;)

I am trying to query a database using the following code (censored):
PHP code:
<?php echo '<table class="sortable2" id="reports" name="reports">
<tr>
<th><font face="Arial, Helvetica, sans-serif" size="3">SteamID</font></th>
<th><font face="Arial, Helvetica, sans-serif" size="3">Server Date/Time</font></th>
<th><font face="Arial, Helvetica, sans-serif" size="3">Server name</font></th>
<th><font face="Arial, Helvetica, sans-serif" size="3">Player name</font></th>
<th><font face="Arial, Helvetica, sans-serif" size="3">Reason</font></th>
<th><font face="Arial, Helvetica, sans-serif" size="3">Reporter</font></th>
</tr>'
;
?>
 
<?
mysql_connect("***","***","****");
   
mysql_select_db("servers");
 
$search=$_POST["search"];
 
$result = mysql_query("SELECT * FROM reports4 WHERE playername LIKE '%$search%'");
 
while($r=mysql_fetch_array($result))
{  
   $steam=$r["steamid"];
   $dt=$r["datetime"];
   $pn=$r["playername"];
   $rs=$r["reason"];
   $rp=$r["reporter"];
   $sn=$r["servername"];
   
  echo "<tr><td> $steam </td><br> <td>$dt </td><td>$sn</td><td>$pn</td><td>$rs</td><td>$rp</td></tr>";
}
 
?>
</table>
 
 

It works fine, however, when searching for someone who has multiple records, it finds all records, but has some huge space before they are displayed. You can try this at http://sammyservers.com/reports/ . Try searching for example "sakura", which works fine, and then try "zombie". You will see the difference.


This post was edited on 11-24-2009 at 08:15 AM by Jimbo.
11-24-2009 08:03 AM
Profile E-Mail PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
29 / Male / Flag
Joined: Jul 2006
RE: PHP parsing certain lines of a text file
would you be able to post all of the code for the search.php page, as the stuff you have posted doesnt seem to be enough and some parts are missing...

right now, i am thinking that it might be some sort of errors that arent getting displayed correctly, but i dont know right now...
11-24-2009 08:40 AM
Profile PM Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
Joined: Mar 2002
RE: PHP parsing certain lines of a text file
HTML code:
</td><br> <td>

What's that <br> doing in there, outside of a table cell? :p
11-24-2009 09:05 AM
Profile PM Web Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On