how would i...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

how would i...

Post by bawla »

be able to 'parse' a .txt file into different parts

Click Here

above is and example for stats of a game

each 'clan' is separated by a '*' and each stat is separated by a ","
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

[php_man]split()[/php_man]
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

ok but how do i get it to do split() off of a .txt file not on my server
like form here
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

open that text file. load everythign into a string. and split it
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

u are getting way too far ahead of me, i've been doing php for 4 or 5 days now, how would i load the .txt file and load it into a string
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

oops! my bad! [php_man]fopen()[/php_man] Also look at all the other file opening functions. Look around the forum and internet also on hwo to open file with PHP. after you've got the file opened, here is som code for you to play around with:

Code: Select all

<?php
//I just copied everything from that text file and put it into a string
$string = "BomB,10262048,77295,23404,1820,397,36*ICW,9079253,62912,21642,2163,322,26*LNo,6807445,45531,16173,1180,151,27*DMW,6647833,43558,16017,1170,149,51*NF,6165175,51434,13709,1055,444,81*CAT,6157036,45272,13588,930,165,29*GOD,5355379,50370,12293,1636,624,58*XLT,5148893,34200,12864,913,151,43*BiB,4455928,33585,10037,673,285,9*BnX,4389378,29339,10975,1002,61,31*WST,4261557,32581,10321,1261,277,40*HiS,4216139,30490,9991,571,86,27*ARMY,2473189,22572,5053,359,281,47*EPP,1491750,10736,3600,289,77,25*MOB,1129248,13549,2564,410,244,48*FaB,999367,7837,2166,102,52,22*USA,783133,6113,1814,125,33,16*FUA,534341,10290,1606,424,554,35*LDC,495464,4432,1199,73,36,10*TFA,437496,2866,998,68,8,6*PPP,347358,3096,821,52,28,2*GOAT,224592,2229,504,37,39,22*GAT,100949,923,279,17,5,8*MFB,98567,2139,310,44,187,14*NONE,45564,3093,369,58,232,35*DTP,38145,387,95,7,4,6*WAR,21408,161,57,4,0,10*9NjA,11815,198,32,3,15,3*bleu,6484,55,13,0,1,6*31,123,2,0,0,0,1*";
$clan = explode("*", $string);
foreach($clan as $val){
	$stats[] = explode(",",$val);
}
echo "<pre>";
print_r($stats);
echo "</pre>";
?>
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

no its alright, its good that your even helping, any help is appreciated
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

one problem with that is that the .txt file is updated everynight because the stats change, that .txt file was espacially made for this kind of thing
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

actually, since your opening from a url, you might find it easier/better to use [php_man]file()[/php_man].
Here try this, i got it to work. It open that text file, reads it into $string, then each element of the main array is a clan and the sub-elements are the clan stats. If that amde any sense...

Code: Select all

<?php
$string = file("http://www.gettiffany.com/groups.txt"); //open the textfile
$string = $string[0]; //its only one line, so get the first line
$clan = explode("*", $string); //explode is pretty much same as split
foreach($clan as $val){                 //loop through each clan
	$stats[] = explode(",",$val);  //and 'explode' the stats!  
}
echo "<pre>";   //formatting to make it easier to read
print_r($stats);  //print the stats array
echo "</pre>";
?>
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

i get something like this

Code: Select all

Array
(
    &#1111;0] =&gt; Array
        (
            &#1111;0] =&gt; BomB
            &#1111;1] =&gt; 10262048
            &#1111;2] =&gt; 77295
            &#1111;3] =&gt; 23404
            &#1111;4] =&gt; 1820
            &#1111;5] =&gt; 397
            &#1111;6] =&gt; 36
        )
but instead of having the
[0] => BomB
[1] => 10262048
[2] => 77295
[3] => 23404
[4] => 1820
[5] => 397
[6] => 36

instead of [0] for it to be Short name of group
and instead of [1] for it to be Points
[2] to be Games Played
[3] to be Games won
[4] to be Zeros
[5] to be Got zeroed
and [6] be Number of members

if that is possible

im guessing on this but , maybe this line could be something like

Code: Select all

$string = $string['Short name of group','Points','Games Played','Games Won','Zeros','Got Zerod','Number of members'];
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

that wouldn't work, because $string is jsut the long string that has all the clans in it. hmm... i dont really know how you'd do that. Ill try some thigns though.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could do this (i think) ;)

Code: Select all

<?php

$fields = array(
  'Short name of Group',
  'Points',
  'Games Played',
  'Games Won',
  'Zeros',
  'Got Zeroed',
  'Number of Members'
);

$clans = explode('*', file_get_contents('http://www.gettiffany.com/groups.txt'));
$count = 0;
foreach($clans as $clan){
  $parts = explode(',', $clan);
  $total = count($parts);
  for($x=0;$x<$total;$x++){
    $output[$count][$fields[$x]] = $parts[$x];
  }
  $count++;
}
var_dump($output); //testing
echo '<br />'.$output[3]['Short name of Group']; //another test
?>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

yeah, mark that works!! you beat me to it! I had the same idea but wasn't at my computer to test it!!
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

all i get is a bunch of nonsense

Code: Select all

array(32) &#123; &#1111;0]=> array(7) &#123; &#1111;"Short name of Group"]=> string(4) "BomB" &#1111;"Points"]=> string(8) "10319814" &#1111;"Games Played"]=> string(5) "77725" &#1111;"Games Won"]=> string(5) "23548" &#1111;"Zeros"]=> string(4) "1827" &#1111;"Got Zeroed"]=> string(3) "402" &#1111;"Number of Members"]=> string(2) "34" &#125; &#1111;1]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "ICW" &#1111;"Points"]=> string(7) "9117444" &#1111;"Games Played"]=> string(5) "63183" &#1111;"Games Won"]=> string(5) "21726" &#1111;"Zeros"]=> string(4) "2169" &#1111;"Got Zeroed"]=> string(3) "325" &#1111;"Number of Members"]=> string(2) "26" &#125; &#1111;2]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "LNo" &#1111;"Points"]=> string(7) "6875353" &#1111;"Games Played"]=> string(5) "45959" &#1111;"Games Won"]=> string(5) "16340" &#1111;"Zeros"]=> string(4) "1194" &#1111;"Got Zeroed"]=> string(3) "152" &#1111;"Number of Members"]=> string(2) "25" &#125; &#1111;3]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "DMW" &#1111;"Points"]=> string(7) "6700591" &#1111;"Games Played"]=> string(5) "43932" &#1111;"Games Won"]=> string(5) "16135" &#1111;"Zeros"]=> string(4) "1179" &#1111;"Got Zeroed"]=> string(3) "153" &#1111;"Number of Members"]=> string(2) "52" &#125; &#1111;4]=> array(7) &#123; &#1111;"Short name of Group"]=> string(2) "NF" &#1111;"Points"]=> string(7) "6342997" &#1111;"Games Played"]=> string(5) "52770" &#1111;"Games Won"]=> string(5) "14103" &#1111;"Zeros"]=> string(4) "1095" &#1111;"Got Zeroed"]=> string(3) "456" &#1111;"Number of Members"]=> string(2) "83" &#125; &#1111;5]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "CAT" &#1111;"Points"]=> string(7) "6175338" &#1111;"Games Played"]=> string(5) "45424" &#1111;"Games Won"]=> string(5) "13622" &#1111;"Zeros"]=> string(3) "932" &#1111;"Got Zeroed"]=> string(3) "166" &#1111;"Number of Members"]=> string(2) "31" &#125; &#1111;6]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "GOD" &#1111;"Points"]=> string(7) "5363640" &#1111;"Games Played"]=> string(5) "50467" &#1111;"Games Won"]=> string(5) "12317" &#1111;"Zeros"]=> string(4) "1636" &#1111;"Got Zeroed"]=> string(3) "629" &#1111;"Number of Members"]=> string(2) "57" &#125; &#1111;7]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "XLT" &#1111;"Points"]=> string(7) "5205374" &#1111;"Games Played"]=> string(5) "34549" &#1111;"Games Won"]=> string(5) "13001" &#1111;"Zeros"]=> string(3) "925" &#1111;"Got Zeroed"]=> string(3) "153" &#1111;"Number of Members"]=> string(2) "44" &#125; &#1111;8]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "BiB" &#1111;"Points"]=> string(7) "4485019" &#1111;"Games Played"]=> string(5) "33769" &#1111;"Games Won"]=> string(5) "10107" &#1111;"Zeros"]=> string(3) "677" &#1111;"Got Zeroed"]=> string(3) "287" &#1111;"Number of Members"]=> string(1) "9" &#125; &#1111;9]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "BnX" &#1111;"Points"]=> string(7) "4392847" &#1111;"Games Played"]=> string(5) "29367" &#1111;"Games Won"]=> string(5) "10985" &#1111;"Zeros"]=> string(4) "1002" &#1111;"Got Zeroed"]=> string(2) "61" &#1111;"Number of Members"]=> string(2) "31" &#125; &#1111;10]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "WST" &#1111;"Points"]=> string(7) "4282069" &#1111;"Games Played"]=> string(5) "32716" &#1111;"Games Won"]=> string(5) "10374" &#1111;"Zeros"]=> string(4) "1268" &#1111;"Got Zeroed"]=> string(3) "278" &#1111;"Number of Members"]=> string(2) "40" &#125; &#1111;11]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "HiS" &#1111;"Points"]=> string(7) "4230438" &#1111;"Games Played"]=> string(5) "30599" &#1111;"Games Won"]=> string(5) "10020" &#1111;"Zeros"]=> string(3) "573" &#1111;"Got Zeroed"]=> string(2) "86" &#1111;"Number of Members"]=> string(2) "27" &#125; &#1111;12]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "NBK" &#1111;"Points"]=> string(7) "2710744" &#1111;"Games Played"]=> string(3) "160" &#1111;"Games Won"]=> string(2) "45" &#1111;"Zeros"]=> string(1) "3" &#1111;"Got Zeroed"]=> string(1) "1" &#1111;"Number of Members"]=> string(2) "10" &#125; &#1111;13]=> array(7) &#123; &#1111;"Short name of Group"]=> string(4) "ARMY" &#1111;"Points"]=> string(7) "2520612" &#1111;"Games Played"]=> string(5) "22901" &#1111;"Games Won"]=> string(4) "5156" &#1111;"Zeros"]=> string(3) "363" &#1111;"Got Zeroed"]=> string(3) "285" &#1111;"Number of Members"]=> string(2) "50" &#125; &#1111;14]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "EPP" &#1111;"Points"]=> string(7) "1493730" &#1111;"Games Played"]=> string(5) "10748" &#1111;"Games Won"]=> string(4) "3605" &#1111;"Zeros"]=> string(3) "289" &#1111;"Got Zeroed"]=> string(2) "77" &#1111;"Number of Members"]=> string(2) "25" &#125; &#1111;15]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "MOB" &#1111;"Points"]=> string(7) "1142245" &#1111;"Games Played"]=> string(5) "13714" &#1111;"Games Won"]=> string(4) "2577" &#1111;"Zeros"]=> string(3) "415" &#1111;"Got Zeroed"]=> string(3) "253" &#1111;"Number of Members"]=> string(2) "50" &#125; &#1111;16]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "FaB" &#1111;"Points"]=> string(7) "1045538" &#1111;"Games Played"]=> string(4) "8203" &#1111;"Games Won"]=> string(4) "2259" &#1111;"Zeros"]=> string(3) "105" &#1111;"Got Zeroed"]=> string(2) "52" &#1111;"Number of Members"]=> string(2) "19" &#125; &#1111;17]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "USA" &#1111;"Points"]=> string(6) "782255" &#1111;"Games Played"]=> string(4) "6139" &#1111;"Games Won"]=> string(4) "1817" &#1111;"Zeros"]=> string(3) "125" &#1111;"Got Zeroed"]=> string(2) "33" &#1111;"Number of Members"]=> string(2) "16" &#125; &#1111;18]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "FUA" &#1111;"Points"]=> string(6) "535327" &#1111;"Games Played"]=> string(5) "10322" &#1111;"Games Won"]=> string(4) "1612" &#1111;"Zeros"]=> string(3) "426" &#1111;"Got Zeroed"]=> string(3) "558" &#1111;"Number of Members"]=> string(2) "33" &#125; &#1111;19]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "911" &#1111;"Points"]=> string(6) "517056" &#1111;"Games Played"]=> string(4) "4536" &#1111;"Games Won"]=> string(4) "1220" &#1111;"Zeros"]=> string(2) "90" &#1111;"Got Zeroed"]=> string(2) "28" &#1111;"Number of Members"]=> string(2) "16" &#125; &#1111;20]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "CoP" &#1111;"Points"]=> string(6) "507094" &#1111;"Games Played"]=> string(4) "4525" &#1111;"Games Won"]=> string(4) "1224" &#1111;"Zeros"]=> string(2) "74" &#1111;"Got Zeroed"]=> string(2) "36" &#1111;"Number of Members"]=> string(1) "8" &#125; &#1111;21]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "TFA" &#1111;"Points"]=> string(6) "438424" &#1111;"Games Played"]=> string(4) "2874" &#1111;"Games Won"]=> string(3) "999" &#1111;"Zeros"]=> string(2) "68" &#1111;"Got Zeroed"]=> string(1) "8" &#1111;"Number of Members"]=> string(1) "5" &#125; &#1111;22]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "PPP" &#1111;"Points"]=> string(6) "347358" &#1111;"Games Played"]=> string(4) "3096" &#1111;"Games Won"]=> string(3) "821" &#1111;"Zeros"]=> string(2) "52" &#1111;"Got Zeroed"]=> string(2) "28" &#1111;"Number of Members"]=> string(1) "2" &#125; &#1111;23]=> array(7) &#123; &#1111;"Short name of Group"]=> string(4) "GOAT" &#1111;"Points"]=> string(6) "234323" &#1111;"Games Played"]=> string(4) "2319" &#1111;"Games Won"]=> string(3) "525" &#1111;"Zeros"]=> string(2) "40" &#1111;"Got Zeroed"]=> string(2) "41" &#1111;"Number of Members"]=> string(2) "28" &#125; &#1111;24]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "GAT" &#1111;"Points"]=> string(6) "110640" &#1111;"Games Played"]=> string(3) "991" &#1111;"Games Won"]=> string(3) "305" &#1111;"Zeros"]=> string(2) "18" &#1111;"Got Zeroed"]=> string(1) "6" &#1111;"Number of Members"]=> string(1) "8" &#125; &#1111;25]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "MFB" &#1111;"Points"]=> string(5) "98567" &#1111;"Games Played"]=> string(4) "2139" &#1111;"Games Won"]=> string(3) "310" &#1111;"Zeros"]=> string(2) "44" &#1111;"Got Zeroed"]=> string(3) "187" &#1111;"Number of Members"]=> string(2) "13" &#125; &#1111;26]=> array(7) &#123; &#1111;"Short name of Group"]=> string(4) "NONE" &#1111;"Points"]=> string(5) "44683" &#1111;"Games Played"]=> string(4) "3126" &#1111;"Games Won"]=> string(3) "370" &#1111;"Zeros"]=> string(2) "58" &#1111;"Got Zeroed"]=> string(3) "235" &#1111;"Number of Members"]=> string(2) "33" &#125; &#1111;27]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "DTP" &#1111;"Points"]=> string(5) "40716" &#1111;"Games Played"]=> string(3) "408" &#1111;"Games Won"]=> string(3) "102" &#1111;"Zeros"]=> string(1) "7" &#1111;"Got Zeroed"]=> string(1) "4" &#1111;"Number of Members"]=> string(1) "6" &#125; &#1111;28]=> array(7) &#123; &#1111;"Short name of Group"]=> string(3) "WAR" &#1111;"Points"]=> string(5) "36503" &#1111;"Games Played"]=> string(3) "270" &#1111;"Games Won"]=> string(2) "91" &#1111;"Zeros"]=> string(1) "8" &#1111;"Got Zeroed"]=> string(1) "0" &#1111;"Number of Members"]=> string(1) "8" &#125; &#1111;29]=> array(7) &#123; &#1111;"Short name of Group"]=> string(4) "9NjA" &#1111;"Points"]=> string(5) "14174" &#1111;"Games Played"]=> string(3) "216" &#1111;"Games Won"]=> string(2) "40" &#1111;"Zeros"]=> string(1) "3" &#1111;"Got Zeroed"]=> string(2) "15" &#1111;"Number of Members"]=> string(1) "3" &#125; &#1111;30]=> array(7) &#123; &#1111;"Short name of Group"]=> string(2) "31" &#1111;"Points"]=> string(3) "123" &#1111;"Games Played"]=> string(1) "2" &#1111;"Games Won"]=> string(1) "0" &#1111;"Zeros"]=> string(1) "0" &#1111;"Got Zeroed"]=> string(1) "0" &#1111;"Number of Members"]=> string(1) "3" &#125; &#1111;31]=> array(1) &#123; &#1111;"Short name of Group"]=> string(0) "" &#125; &#125; 
DMW
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

That's the result of the var_dump() to show you the format of the array, which is now in the format you wanted. Do what you like with the array now it's how you want it.

(that's why i marked those two lines as test lines .. as they are for...testing ;))
Post Reply