php - Building on file formats with text -
evening stack,
so i'm building own little custom file type site use can pull out data file , place appropiate areas of website. i'm able save file format , open , extract data file. seem having trouble locolising data want file. i've seperated each set of data ';'. here reading code.
function read_suggestion($suggest) { // global variables global $sug_reports; // file need open $sugid = $suggest; $filename = "reports/suggestions/" . $sug_reports[$sugid]; // open file $handler = fopen($filename, "a+"); // load file variable $raw_data = fread($handler, filesize($filename)); // first lets set these indexes $file_data = array ("title" => "", "date_submitted" => "", "data" => "", "by" => ""); // find title $current = 0; $first_stop = strpos($raw_data, ";"); $last_stop = strpos($raw_data, ";", $first_stop); // read first stop make sure it's right tag $read_first = substr($raw_data, $current, $first_stop); // if right string can load following data array if ($read_first == "suggestion title") { // read data $first_stop = (strpos($raw_data, ";", 49) +1); $last_stop = strpos($raw_data, ";", $first_stop); // read title data $file_data['title'] = substr($raw_data, $first_stop, $last_stop); echo $first_stop . "<br />"; echo $last_stop . "<br />"; echo $file_data['title'] . "<br />"; } else { echo "file currupted"; } }
the file looks bit this
suggestion title;make suggestion reading possible on admin section; suggestion from;bob; suggestion submitted;20/07/2011 10:52; data;hello world; end of file;
the output of above code this
67 84 suggestion from;bob; suggestion submitted;20/07/2011 10:52; data;hello world!; end o
am doing wrong?
edit: nevermind, miss understanding of how substr worked. correct code getting wanted below
$first_stop = (strpos($raw_data, ";", $last_stop) +1); $last_stop = (strpos($raw_data, ";", $first_stop) - $first_stop); // read title data $file_data['title'] = substr($raw_data, $first_stop, $last_stop);
Comments
Post a Comment