ShivaGupta Posted June 21, 2013 Share Posted June 21, 2013 (edited) i am using this to get data frm text file...... text file content.345in 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 June 21, 2013 by ShivaGupta Quote Link to comment https://forums.phpfreaks.com/topic/279421-need-help-file_get_contentstesttxt/ Share on other sites More sharing options...
litebearer Posted June 21, 2013 Share Posted June 21, 2013 hint key word : explode Quote Link to comment https://forums.phpfreaks.com/topic/279421-need-help-file_get_contentstesttxt/#findComment-1437227 Share on other sites More sharing options...
ShivaGupta Posted June 21, 2013 Author Share Posted June 21, 2013 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/279421-need-help-file_get_contentstesttxt/#findComment-1437234 Share on other sites More sharing options...
Solution Irate Posted June 21, 2013 Solution Share Posted June 21, 2013 (edited) 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 June 21, 2013 by Irate Quote Link to comment https://forums.phpfreaks.com/topic/279421-need-help-file_get_contentstesttxt/#findComment-1437237 Share on other sites More sharing options...
AbraCadaver Posted June 21, 2013 Share Posted June 21, 2013 Or just: $fh = explode(',', file_get_contents('test.txt')); Quote Link to comment https://forums.phpfreaks.com/topic/279421-need-help-file_get_contentstesttxt/#findComment-1437268 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.