Jump to content

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


ShivaGupta

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
 

Link to comment
https://forums.phpfreaks.com/topic/279421-need-help-file_get_contentstesttxt/
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));
?> 

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"
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.