.josh Posted July 13, 2010 Share Posted July 13, 2010 are you even using the right path/to/file? do this: echo $data; do you see your stuff outputting on the screen? If so... do this: $data = file("Matthew.txt"); echo "<pre>"; print_r($data); And post here what it outputs. Quote Link to comment Share on other sites More sharing options...
ajicles Posted July 13, 2010 Author Share Posted July 13, 2010 I did echo it back, I'll try that code. Why can't I use like use (below) it is easier for me to understand lol. Just make an array to get rid of the stuff I don't want lol. $data = file("Matthew.txt"); $data = str_replace("character-username = ","",$data[1]); echo $data; The code above returns: Matthew That code you posted above returns: Array ( [0] => [ACCOUNT] [1] => character-username = Matthew [2] => character-password = 53552e2db2937526b0766ce1e1eb1ffd [3] => [4] => [CHARACTER] [5] => character-height = 0 [6] => character-posx = 2558 [7] => character-posy = 3090 [8] => character-rights = 3 [9] => character-lastconnection = [10] => character-special = 4225 [11] => character-tz = -1 [12] => character-starter = 1 [13] => character-pouch1 = 0 [14] => character-pouch2 = 0 [15] => character-pouch3 = 0 [16] => character-pouch4 = 0 [17] => character-energy = 100 [18] => character-expLock = false [19] => character-taskID = -1 [20] => character-taskAmount = -1 [21] => character-jad = 1 [22] => character-Ahrim = 0 [23] => character-ce = 0 [24] => character-sp = 0 [25] => character-aa = 0 [26] => character-Verac = 0 [27] => character-Dharok =0 [28] => character-Torag = 0 [29] => character-Karils =0 [30] => character-Guthan =0 [31] => character-Barrows 0 [32] => character-brightness = 0 [33] => character-fighttype = 3 [34] => character-skill = 3 [35] => character-chat = 1 [36] => character-ancients = 2 [37] => character-pin = 0 [38] => character-pkPoints = 1059 [39] => character-saradomin = 0 [40] => character-zammykills =60 [41] => character-retaliate = 0 [42] => character-action = 0 [43] => character-pcpoints = 799 [44] => character-duelrating0 [45] => character-duelpoints0 [46] => character-assault = 1 [47] => character-donator = 1 [48] => character-bow = 100 [49] => character-shield = 100 [50] => character-tradeTimer = 0 [51] => character-tb = 0 [52] => character-q15 = 0 [53] => character-q14 = 0 [54] => character-q13 = 0 [55] => character-q12 = 0 [56] => [57] => [EQUIPMENT] [58] => character-equip = 0 6182 1 [59] => character-equip = 1 4359 1 [60] => character-equip = 2 1725 1 [61] => character-equip = 3 14013 1 [62] => character-equip = 4 6916 1 [63] => character-equip = 5 -1 0 [64] => character-equip = 6 -1 0 [65] => character-equip = 7 2497 1 [66] => character-equip = 8 -1 0 [67] => character-equip = 9 7461 1 [68] => character-equip = 10 2904 1 [69] => character-equip = 11 -1 0 [70] => character-equip = 12 -1 0 [71] => character-equip = 13 -1 0 [72] => [73] => [LOOK] [74] => character-look = 0 0 [75] => character-look = 1 3 [76] => character-look = 2 14 [77] => character-look = 3 18 [78] => character-look = 4 26 [79] => character-look = 5 33 [80] => character-look = 6 36 [81] => character-look = 7 42 [82] => character-look = 8 0 [83] => character-look = 9 1 [84] => character-look = 10 2 [85] => character-look = 11 0 [86] => character-look = 12 0 [87] => character-look = 13 0 [88] => character-look = 14 0 [89] => character-look = 15 0 [90] => character-look = 16 0 [91] => character-look = 17 0 [92] => character-look = 18 0 [93] => [94] => [sKILLS] [95] => character-skill = 0 69 28673300 [96] => character-skill = 1 99 28675400 [97] => character-skill = 2 99 32294500 [98] => character-skill = 3 99 32300555 [99] => character-skill = 4 99 28000000 [100] => character-skill = 5 99 28263340 [101] => character-skill = 6 99 28000000 [102] => character-skill = 7 99 28000000 [103] => character-skill = 8 99 28000000 [104] => character-skill = 9 99 28000000 [105] => character-skill = 10 99 28000000 [106] => character-skill = 11 99 28000000 [107] => character-skill = 12 99 28000000 [108] => character-skill = 13 99 28020000 [109] => character-skill = 14 99 28007000 [110] => character-skill = 15 99 28000000 [111] => character-skill = 16 99 28000000 [112] => character-skill = 17 99 29342795 [113] => character-skill = 18 99 28000000 [114] => character-skill = 19 99 28000000 [115] => character-skill = 20 99 28000000 [116] => character-skill = 21 1 0 [117] => character-skill = 22 1 0 [118] => character-skill = 23 1 0 [119] => character-skill = 24 0 0 [120] => [121] => [iTEMS] [122] => character-item = 0 9002 1 [123] => character-item = 1 1276 1 [124] => [125] => [bANK] [126] => character-bank = 0 4358 1 [127] => character-bank = 1 4336 2 [128] => character-bank = 2 14012 3 [129] => character-bank = 3 344 1 [130] => character-bank = 4 4326 1 [131] => character-bank = 5 14058 1 [132] => character-bank = 6 4332 1 [133] => character-bank = 7 4338 1 [134] => character-bank = 8 14014 2 [135] => character-bank = 9 4328 1 [136] => character-bank = 10 4362 1 [137] => character-bank = 11 4086 1 [138] => character-bank = 12 4334 1 [139] => character-bank = 13 14063 1 [140] => character-bank = 14 4342 2 [141] => character-bank = 15 4318 1 [142] => character-bank = 16 7612 8 [143] => character-bank = 17 4340 1 [144] => character-bank = 18 2435 27 [145] => character-bank = 19 4344 2 [146] => character-bank = 20 4348 1 [147] => character-bank = 21 4352 1 [148] => character-bank = 22 4330 1 [149] => character-bank = 23 2348 1 [150] => character-bank = 24 1334 1 [151] => character-bank = 25 996 2000045851 [152] => character-bank = 26 386 1013 [153] => character-bank = 27 4320 2 [154] => character-bank = 28 4360 1 [155] => character-bank = 29 4346 1 [156] => character-bank = 30 4322 1 [157] => character-bank = 31 4354 1 [158] => character-bank = 32 551 1 [159] => character-bank = 33 1541 1 [160] => character-bank = 34 747 1 [161] => character-bank = 35 2905 1 [162] => character-bank = 36 2492 1 [163] => character-bank = 37 6819 1 [164] => character-bank = 38 4566 1 [165] => character-bank = 39 4356 1 [166] => character-bank = 40 563 3010 [167] => character-bank = 41 557 5000 [168] => character-bank = 42 555 4000 [169] => character-bank = 43 566 4000 [170] => character-bank = 44 561 3000 [171] => character-bank = 45 556 4000 [172] => character-bank = 46 1049 1 [173] => character-bank = 48 4152 1 [174] => character-bank = 49 1043 1 [175] => character-bank = 50 651 1 [176] => character-bank = 51 641 1 [177] => character-bank = 52 6542 1 [178] => character-bank = 53 4098 1 [179] => character-bank = 54 4096 1 [180] => character-bank = 55 14047 1 [181] => character-bank = 56 14029 1 [182] => character-bank = 57 14051 1 [183] => character-bank = 58 14032 1 [184] => character-bank = 59 9005 1 [185] => character-bank = 60 14031 2 [186] => character-bank = 61 14067 1 [187] => character-bank = 62 14066 1 [188] => character-bank = 63 14039 1 [189] => character-bank = 64 14041 1 [190] => character-bank = 65 14064 1 [191] => character-bank = 66 7807 1 [192] => character-bank = 67 7808 1 [193] => character-bank = 68 7450 1 [194] => character-bank = 69 14049 1 [195] => character-bank = 70 4676 1 [196] => character-bank = 71 14040 1 [197] => character-bank = 72 4068 2 [198] => character-bank = 73 14071 1 [199] => character-bank = 74 14070 1 [200] => character-bank = 75 6571 1 [201] => character-bank = 76 14054 1 [202] => character-bank = 78 14050 1 [203] => character-bank = 79 14065 1 [204] => character-bank = 80 14061 1 [205] => character-bank = 81 14010 1 [206] => character-bank = 82 14053 1 [207] => [208] => [FRIENDS] [209] => character-friend = 0 3458605949762185758 [210] => character-friend = 1 904262259 [211] => character-friend = 2 918897639 [212] => character-friend = 3 41091004572 [213] => character-friend = 4 19021682 [214] => character-friend = 5 453997938233492 [215] => character-friend = 6 59658913159 [216] => character-friend = 7 3470371525163566 [217] => character-friend = 8 279971649 [218] => character-friend = 9 601731966 [219] => [220] => [iGNORES] [221] => [222] => [EOF] [223] => ) Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 13, 2010 Share Posted July 13, 2010 Why can't I use like use (below) it is easier for me to understand lol. Just make an array to get rid of the stuff I don't want lol. $data = file("Matthew.txt"); $data = str_replace("character-username = ","",$data[1]); echo $data; Use whatever you want to use. No one is forcing you to use anything. We have provided code that works on the file you have provided it. I tested the code I posted back on reply #5 and it worked. You were the one asking for help using a regular expression. If you don't have the mental capacity or the initiative to learn something then don't post on a forum asking for people to help. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 13, 2010 Share Posted July 13, 2010 Here is my last submission to this thread. This solution uses - surprise, surprise - regular expressions. Because, well, it makes sense for this problem. But, no one is forcing you to use it. With this solution you can add additional keys and expressions to the $patterns array to have whatever informaiton you want returned in the output array: function parser($data) { $output = array(); $patterns = array( 'username' => '/character-username = (.*)/', 'skill-0' => '/character-skill = 0\t([^\t].)/' ); foreach($patterns as $key => $pattern) { preg_match($pattern, $data, $matches); $output[$key] = $matches[1]; } return $output; } $data = file_get_contents('Matthew.txt'); print_r(parser($data)); Using the text file you attached, this is the output: Array ( [username] => Matthew [skill-0] => 99 ) Quote Link to comment Share on other sites More sharing options...
ajicles Posted July 13, 2010 Author Share Posted July 13, 2010 Ok I have done a couple of things and my script works. I just want to know to use something like str_replace to remove the last data after the indent in a array. So in the return example is $array[1] after being stripped by str_replace outputs: Skill 1: 69(indent)28673300. I want to remove the indent and last numbers. Code: $data = file("Matthew.txt"); $counter = 0; $array = array( "character-username = ", "character-skill = 0 ", "character-skill = 1 ", "character-skill = 2 ", "character-skill = 3 ", "character-skill = 4 ", "character-skill = 5 ", "character-skill = 6 ", "character-skill = 7 ", "character-skill = 8 ", "character-skill = 9 ", "character-skill = 10 ", "character-skill = 11 ", "character-skill = 12 ", "character-skill = 13 ", "character-skill = 14 ", "character-skill = 15 ", "character-skill = 16 ", "character-skill = 17 ", "character-skill = 18 ", "character-skill = 19 ", "character-skill = 20 ", "character-skill = 21 ", "character-skill = 22 ", "character-skill = 23 ", "character-skill = 24 "); $iii = 1; $output = str_replace($array[0],"",$data[1]); echo "Username: ".$output.'<br>'; for ($i = 95; $i <= 119; $i++) { $output = str_replace($array[$iii],"",$data[$i]); echo "Skill $iii: ".$output.'<br>'; $iii = $iii + 1; } Which Returns: Username: Matthew Skill 1: 69 28673300 Skill 2: 99 28675400 Skill 3: 99 32294500 Skill 4: 99 32300555 Skill 5: 99 28000000 Skill 6: 99 28263340 Skill 7: 99 28000000 Skill 8: 99 28000000 Skill 9: 99 28000000 Skill 10: 99 28000000 Skill 11: 99 28000000 Skill 12: 99 28000000 Skill 13: 99 28000000 Skill 14: 99 28020000 Skill 15: 99 28007000 Skill 16: 99 28000000 Skill 17: 99 28000000 Skill 18: 99 29342795 Skill 19: 99 28000000 Skill 20: 99 28000000 Skill 21: 99 28000000 Skill 22: 1 0 Skill 23: 1 0 Skill 24: 1 0 Skill 25: 0 0 Thanks ~AJ Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 13, 2010 Share Posted July 13, 2010 My god, this is frustrating. I don't want to feel that all the time I've invested has been wasted. If I was able to "unfollow" a thread I would, so I wouldn't be tempted to see these responses. I just handed you the perfect solution and you didn't take 2 seconds to even understand or implement it. All you need to do is use the function I provided and add the additional patterns for the other skill levels. You could have easily modified the one I provided for skill-0 to get the others. In fact, you could create them programatically function parser($data) { //Temp array for the output $output = array(); //Create array of the keys and patters to extract data $patterns = array( 'username' => '/character-username = (.*)/', ); //Create skill patterns programatically since they are the same for($skillIdx=0; $skillIdx<25; $skillIdx++) { $patterns["skill-{$skillIdx}"] = '/character-skill = '.$skillIdx.'\t([^\t]*)/'; } //Iterate through pattern array to extract data and assign to $ouput var foreach($patterns as $key => $pattern) { preg_match($pattern, $data, $matches); $output[$key] = $matches[1]; } //Return the output array return $output; } $data = file_get_contents('Matthew.txt'); print_r(parser($data)); That function will extract ONLY the data you are after so there is no need to strip unnecessary data afterward. Output: Array ( [username] => Matthew [skill-0] => 99 [skill-1] => 99 [skill-2] => 99 [skill-3] => 99 [skill-4] => 99 [skill-5] => 99 [skill-6] => 99 [skill-7] => 99 [skill-8] => 99 [skill-9] => 99 [skill-10] => 99 [skill-11] => 99 [skill-12] => 99 [skill-13] => 99 [skill-14] => 99 [skill-15] => 99 [skill-16] => 99 [skill-17] => 99 [skill-18] => 99 [skill-19] => 99 [skill-20] => 99 [skill-21] => 1 [skill-22] => 1 [skill-23] => 1 [skill-24] => 0 ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.