这是上传和打印文本文件。现在,这个txt文件包含预订人员的详细信息,并通过电子邮件发送,如下所示:
some text (always the same)
= ================
text again
name : Mr F Someone
address: Somewhere
...
...
more same text
问题是没有分隔符。我想抓住像“先生”,“F”,“某人”这样的数据......然后把它们中的每一个都放在一个表格的输入字段中。我试过这样的事情:
$section_a = file_get_contents($up_folder, NULL, NULL, 508, 2);
echo '<input type="text" name="Title" value="'.$section_a.'">';
但是如果它是“Miss”而不是“Mr”那么它就是一个问题,与名称的长度相同,等等。并且文件中有多个“预订”。那么,有没有办法从文件中获取每个预订的详细信息,以便每次都放在一个表单中?
<?php
if($_FILES['userfile']['error'] > 0){
echo 'Houston, we have a problem: ';
switch($_FILES['userfile']['error'])
{
case 1: echo 'file too big'; break;
case 2: echo 'file too big'; break;
case 3: echo 'incomplete upload'; break;
case 4: echo 'uplad error'; break;
}
exit;
}
if($_FILES['userfile']['type'] != 'text/plain'){
echo 'Ooh noes! File is not plain text dude!';
exit;
}
$up_folder = 'upload/'.$_FILES['userfile']['name'];
if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $up_folder)){
echo 'cannot move file';
exit;
}
}
else{
echo 'error: ';
echo $_FILES['userfile']['name'];
exit;
}
echo 'file upload successful<br>';
$fp = fopen($up_folder, 'r');
$contents = fread($fp, filesize($up_folder));
fclose($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);
echo 'Uploaded file content: <br> <br>';
echo $contents;
?>
答案 0 :(得分:0)
$fetch = ['name','address'];
$output = [];
$fp = fopen($file, 'r');
while(!feof($fp) && count($output) !== count($fetch)) {
$line = fgets($line);
list($prefix, $value) = explode(':', $line, 2);
$prefix = strtolower(trim($prefix));
foreach($fetch as $f) {
if($prefix === $f) {
$output[$f] = trim($value);
break;
}
}
}
fclose($fp);
print_r($output);