Вечерний стек,
Итак, в настоящее время я создаю свой собственный небольшой тип файла для моего сайта, чтобы я мог извлечь данные из файла и поместить его в подходящие области сайта.Я могу сохранить файл в формате и открыть и извлечь данные из файла.У меня возникли проблемы с поиском нужных мне данных из файла.Я разделил каждый набор данных с помощью «;».Вот код чтения.
function read_suggestion($suggest) {
// Get Global variables
global $sug_reports;
// Get file we need to open
$sugid = $suggest;
$filename = "reports/suggestions/" . $sug_reports[$sugid];
// Open The file
$handler = fopen($filename, "a+");
// Load the file into an 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 to make sure it's the right tag
$read_first = substr($raw_data, $current, $first_stop);
// If it is the right string then we can load the following data into the array
if ($read_first == "Suggestion Title") {
// Read data
$first_stop = (strpos($raw_data, ";", 49) +1);
$last_stop = strpos($raw_data, ";", $first_stop);
// Read the 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";
}
}
Файл выглядит примерно так
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;
Вывод этого кода выглядит следующим образом:
67 84Предложение От; Боб;Предложение отправлено; 20/07/2011 10:52;Данные; Hello World !;Конец o
я делаю что-то не так?
РЕДАКТИРОВАТЬ: не важно, пропустите понимание того, как работает substr.правильный код для получения того, что я хотел ниже
$first_stop = (strpos($raw_data, ";", $last_stop) +1);
$last_stop = (strpos($raw_data, ";", $first_stop) - $first_stop);
// Read the title data
$file_data['title'] = substr($raw_data, $first_stop, $last_stop);