Jump to content

[SOLVED] PHP, TXT File Read


jkewlo

Recommended Posts

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

Link to comment
Share on other sites

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()).

Link to comment
Share on other sites

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);

 

??

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.