jkewlo Posted May 16, 2009 Share Posted May 16, 2009 hey guys, Kinda wondering on how I should go about doing this. I have a txt file which I want to read in the txt file it has 501# A potion made from ground Red Herbs that restores ^000088about 45 HP^000000. ^ffffff_^000000 Weight :^777777 7^000000 # how would I go about pulling the info via the id 501 there is a database with these ID's to match but no description field. if you could help me that would be great Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/ Share on other sites More sharing options...
Masna Posted May 16, 2009 Share Posted May 16, 2009 fread(). You'll have to parse the return string manually. Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-835501 Share on other sites More sharing options...
jkewlo Posted May 16, 2009 Author Share Posted May 16, 2009 can you give me some example please Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-835508 Share on other sites More sharing options...
Masna Posted May 16, 2009 Share Posted May 16, 2009 An example of what? Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-835510 Share on other sites More sharing options...
jkewlo Posted May 16, 2009 Author Share Posted May 16, 2009 Well here is the link I have saved the txt file to my computer to run from my localhost http://opensvn.csie.org/EnglishTranslation/English/idnum2itemdesctable.txt How would I go about finding the correct id in a txt file. I have never used fread() and would be completly lost as in how to do it Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-835512 Share on other sites More sharing options...
Masna Posted May 16, 2009 Share Posted May 16, 2009 I've never used fread() either. That's what the docs are for. php.net/fread Anyway... fread returns a string that contains the contents of the file. So... $contents_of_file = fread("file.txt"); From there, you can very easily parse $contents_of_file (using, most likely, explode()). Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-835514 Share on other sites More sharing options...
jkewlo Posted May 17, 2009 Author Share Posted May 17, 2009 Yeah, So I can not figure this out. How will I be able to read the certain line? in a txt file? Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836020 Share on other sites More sharing options...
.josh Posted May 17, 2009 Share Posted May 17, 2009 $file = file_get_contents('filename.txt'); preg_match('~501#[^#]*#~s',$file,$match); Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836028 Share on other sites More sharing options...
jkewlo Posted May 17, 2009 Author Share Posted May 17, 2009 Ah kick ass so would it be possible to do like $id = $_GET['id']; $sql = "SELECT * FROM item_db WHERE id = '". $id ."'"; $result=mysql_query($sql); while($rows = mysql_fetch_array($result)){ $file = file_get_contents('filename.txt'); preg_match('~'". $id ."#[^#]*#~s',$file,$match); ?? Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836033 Share on other sites More sharing options...
.josh Posted May 17, 2009 Share Posted May 17, 2009 it's a good thing you went ahead and asked instead of just trying it out, because running that code would have blown up your server. Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836038 Share on other sites More sharing options...
BobcatM Posted May 17, 2009 Share Posted May 17, 2009 How about trying to run it and see what you get? Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836040 Share on other sites More sharing options...
jkewlo Posted May 17, 2009 Author Share Posted May 17, 2009 well ill run it on my localhost?!? what do u mean it will blow up my server? Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836042 Share on other sites More sharing options...
.josh Posted May 17, 2009 Share Posted May 17, 2009 Your server is the computer you are running the script on, as it requires a server to run it. And I thought that the concept of something blowing up was rather straight forward. Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836046 Share on other sites More sharing options...
BobcatM Posted May 17, 2009 Share Posted May 17, 2009 what do u mean it will blow up my server? haha... Crayon you are awsome. Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836047 Share on other sites More sharing options...
jkewlo Posted May 17, 2009 Author Share Posted May 17, 2009 lol, well i know what a server is and im running it on my local host then will drop it on my server. but when someone sways blowing things up i tend to duck >< Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836048 Share on other sites More sharing options...
jkewlo Posted May 17, 2009 Author Share Posted May 17, 2009 I get a parser error on line 86 </td><td colspan=9 class='bb' valign='top'>". $file = file_get_contents('idnum2itemdesctable.txt'); line 86: preg_match('~501#[^#]*#~s',$file); ."</td></tr> Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836050 Share on other sites More sharing options...
jkewlo Posted May 17, 2009 Author Share Posted May 17, 2009 I can get it to display by $file but that does the whole txt file.and if i do $match = preg_match then it will display 1 or 0 this is the format for everything 501# A potion made from ground Red Herbs that restores ^000088about 45 HP^000000. ^ffffff_^000000 Weight :^777777 7^000000 # 502# A potion made from ground Red and Yellow Herbs that restores ^000088about 105 HP^000000. ^ffffff_^000000 Weight :^777777 10^000000 # 503# A potion made from ground Yellow Herbs that restores ^000088about 175 HP^000000. ^ffffff_^000000 Weight :^777777 13^000000 # 504# A potion made from ground White Herbs that restores ^000088about 325 HP^000000. ^ffffff_^000000 Weight :^777777 15^000000 # 505# A potion made from ground Blue Herbs that restores ^000088about 60 SP^000000. ^ffffff_^000000 Weight :^777777 15^000000 # 506# A potion which cures ^000088Poison^000000, ^000088Silence^000000, ^000088Blind^000000 and ^000088Confusion^000000 effects. ^ffffff_^000000 Weight :^777777 7^000000 # say you look up Green Potion being id 506 and in the txt file it is 506# I cant seem to get it to work, and I dont want to add all this into the database Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836054 Share on other sites More sharing options...
.josh Posted May 17, 2009 Share Posted May 17, 2009 That's because preg_match returns true on match, false on not match, if you assign it to a variable. The 3rd argument is where it assigns what it matched. Coincidentally, that's where I put $match. One of the great things about the php manual is that you can do for instance, http://www.php.net/preg_match and it will take you straight to the manual entry for the function. And one of the great things about the manual is that it tells you all kinds of neat things like what functions there are, what they do, how to use them, etc... it's a revolutionary concept that I hope catches on, because of its superb usefulness. Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836059 Share on other sites More sharing options...
jkewlo Posted May 18, 2009 Author Share Posted May 18, 2009 yeah, I tend to go there. but I also tend to get lost in all of it ><, I will fiddle with this some more and try and get this! if you have anymore suggestions please let me know Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836078 Share on other sites More sharing options...
.josh Posted May 18, 2009 Share Posted May 18, 2009 if you have anymore suggestions please let me know You mean, other than telling you why what you did won't work, what you should do, and providing you a reference link? Sorry, think that's about the end of the line. Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836081 Share on other sites More sharing options...
jkewlo Posted May 18, 2009 Author Share Posted May 18, 2009 It's now printing ARRAY Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836092 Share on other sites More sharing options...
.josh Posted May 18, 2009 Share Posted May 18, 2009 That's because preg_match returns an array, just like the manual says. Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836112 Share on other sites More sharing options...
jkewlo Posted May 18, 2009 Author Share Posted May 18, 2009 so I can get it to display at random, but cannot seem to get it to select by the id in the txt file. I have read the manual with no luck $file = file_get_contents('idnum2itemdesctable.txt'); $array = explode("". $row->id ."", $file); $random = rand(0, count($array)); $desc = $array[$random]; Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-836746 Share on other sites More sharing options...
jkewlo Posted May 21, 2009 Author Share Posted May 21, 2009 yeah, So sorry to come back to this, I have tried several things with no luck still, Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-838608 Share on other sites More sharing options...
Maq Posted May 21, 2009 Share Posted May 21, 2009 yeah, So sorry to come back to this, I have tried several things with no luck still, Sure. You want a random id i.e. - 501#, 1064#. Assuming you the ids are always 3-4 digits long and end with a #, you can you this: $file = file_get_contents('http://opensvn.csie.org/EnglishTranslation/English/idnum2itemdesctable.txt'); preg_match_all("~\d{3,4}#~", $file, $matches); $random = rand(0, count($matches[0])-1); $desc = $matches[0][$random]; echo $desc; ?> Why aren't you storing all this in a database? Quote Link to comment https://forums.phpfreaks.com/topic/158423-solved-php-txt-file-read/#findComment-838634 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.