PHP parsing certain lines of a text file - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP parsing certain lines of a text file (/showthread.php?tid=92982)
PHP parsing certain lines of a text file by Jimbo on 11-22-2009 at 07:13 PM
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
RE: PHP parsing certain lines of a text file by John Anderton on 11-22-2009 at 07:22 PM
Use regexp and read up on your preg_match_all
Should be simple enough to do
RE: PHP parsing certain lines of a text file by prashker on 11-22-2009 at 07:59 PM
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
RE: PHP parsing certain lines of a text file by Jimbo on 11-23-2009 at 12:07 AM
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
RE: PHP parsing certain lines of a text file by NanaFreak on 11-23-2009 at 12:36 AM
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 [=
RE: PHP parsing certain lines of a text file by Jimbo on 11-23-2009 at 11:45 PM
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?
RE: PHP parsing certain lines of a text file by NanaFreak on 11-24-2009 at 12:32 AM
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 [=
RE: PHP parsing certain lines of a text file by Jimbo on 11-24-2009 at 08:03 AM
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.
RE: PHP parsing certain lines of a text file by NanaFreak on 11-24-2009 at 08:40 AM
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...
RE: PHP parsing certain lines of a text file by WDZ on 11-24-2009 at 09:05 AM
html code: </td><br> <td>
What's that <br> doing in there, outside of a table cell?
RE: PHP parsing certain lines of a text file by Jimbo on 11-24-2009 at 03:22 PM
@Nana, what I posted above is the whole search.php.
I use
html code: <form method="post" action="search.php">
<input type="text" value="Enter Player Name" name="search" size=25 maxlength=25>
<input type="Submit" name="Search" value="Search">
</form>
on the main page.
I dont know how I missed that <br>, that fixed it Thanks WDZ
RE: PHP parsing certain lines of a text file by NanaFreak on 11-25-2009 at 12:38 AM
quote: Originally posted by Jimbo
@Nana, what I posted above is the whole search.php.
it isnt because nowhere on that page is there this html:
html code: <div id="header">
<a href="http://sammyservers.com/reports/?sort=33">Return to Reports table</a>
</div>
RE: PHP parsing certain lines of a text file by John Anderton on 11-25-2009 at 06:29 AM
quote: Originally posted by WDZ
html code: </td><br> <td>
What's that <br> doing in there, outside of a table cell?
Well spotted, sir. You deserve a cookie!
RE: RE: PHP parsing certain lines of a text file by Jimbo on 11-25-2009 at 07:57 AM
quote: Originally posted by NanaFreak
quote: Originally posted by Jimbo
@Nana, what I posted above is the whole search.php.
it isnt because nowhere on that page is there this html:
html code: <div id="header">
<a href="http://sammyservers.com/reports/?sort=33">Return to Reports table</a>
</div>
Well, I removed the 2 div elements as they wouldnt matter, but apart from that, its the whole code
RE: PHP parsing certain lines of a text file by NanaFreak on 11-25-2009 at 09:24 AM
btw, i just tested it again and it worked fine... =\
RE: PHP parsing certain lines of a text file by segosa on 12-14-2009 at 11:15 PM
Also, read about sql injection.
|