Jump to content

need help file_get_contents("test.txt");....


ShivaGupta
Go to solution Solved by Irate,

Recommended Posts

i am using this to get data frm text file......

text file content.
345


in php

 

$fh=file_get_contents("test.txt");

and it working fine.



but here what i want.


text file content.
345,567,789,


in php

 

$fh="345";
$ch="567";
$dh="789";

so plz give me example how to get content.


thanx
 

Edited by ShivaGupta
Link to comment
Share on other sites

hint key word : explode

YA i found this  but their is no intrigation into text file.

<?php
$str = 'one,two,three,four';

// zero limit
print_r(explode(',',$str,0));

// positive limit
print_r(explode(',',$str,2));

// negative limit
print_r(explode(',',$str,-1));
?> 
Link to comment
Share on other sites

  • Solution

You want to open a file and read its context, then explode on it? Easy.

 

<?php

function gc($file) {
$stream = fopen($file,"r");
if(!feof($stream)) {
$content = file_get_contents($file);
$close = fclose($stream);
return $content;
} else { return false; }
}

$c = gc("test.txt");

if($c) {
$arr = explode(",",$c,2);
}
print_r($arr);
?>
That should work.

 

Edit: And you can easily fetch the content from the array now.

 

<?php
$first = $array[0]; // "345"
$second = $array[1]; // "567"
$third = $array[2]; // "789"
?>
Edited by Irate
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.