以文本为基础构建文件格式

时间:2011-07-21 05:52:32

标签: php string

晚上筹码,

所以我正在为我的网站构建我自己的小自定义文件类型,以便我可以从文件中提取数据并将其放入网站的适当区域。我能够将文件保存为格式并打开并从文件中提取数据。我似乎无法从文件中定位我想要的数据。我用';'分隔了每组数据。这是阅读代码。

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);

0 个答案:

没有答案