所以我有两个文本文件,一个叫做 users.txt 和 schedule.txt。 schedule.txt 看起来像这样:(注意上面的数字不在文件中,它们用作用户文件的引用)
0 1 2
2013-04-11^12:00|4:00|14:00
3 4 5 6
2013-07-21^10:00|15:00|18:00|15:00
7 8
2013-12-11^13:00|14:00
users.txt 文件如下所示:
约翰·史密斯^2|4|6
星球大战^0|1|3|6
卢克·天行者^2|6|7|8
并且这些数字中的每一个都对应于另一个文件的索引...所以例如 John Smith 的时间是 2013-04-11 的 14:00(从 2),时间是 15:00(从4) 等等...
好吧,我使用 HTML 为所有这些构建了一个表,但我无法提取时间值(例如 12:00)。
此外,我对 html 和 PHP 还很陌生,所以请放轻松...如果您有指示或任何其他有用的信息,请分享 =)当我尝试添加编辑时,我肯定会需要它(对于使用单选按钮编辑时间)并添加新用户。
非常感谢您的帮助/建议!
这是我的代码:
<?php
<!DOCTYPE html>
<html>
<head>
<title>Scheduler</title>
</head>
<body>
<h2>
<center>Select Your Meeting Times</center></h2>
<table border="1"
cellpadding="10">
<tr>
<th>User</th>
<th>Action</th>
<?php
//error_reporting(0);
date_default_timezone_set('America/New_York');
// used for displaying schedule times
$schedule= file('schedule.txt');
// loop through array and output contents
foreach($schedule as $s) {
list($year, $month, $day) = explode('-', $s);
$ga = explode("|", $s);
var_dump($year);
//echo $ga[5];
$year= intval($year, 10);
$month= intval($month, 10);
$day= intval($day, 10);
$h = mktime(0, 0, 0, $month, $day,$year);
$d = date("F dS, Y", $h); //used to get the day of the week
$w= date("l", $h); // w now holds the day of the week.
foreach($schedule as $t)
{
// $split = preg_split('/| /', $t, -1, PREG_SPLIT_NO_EMPTY);
list($time) = explode("|", $t);
list($time1) = explode(" ", $time);
// var_dump($time1);
// $output = trim($time1);
// echo $test;
echo "<th>" . $w . "<br>" . $month . "/" . $day . "/" . $year . "</th>\n";
}
}
?>
</tr>
<tr>
<?php
$text = file('user.txt');
// loop through array and output contents
foreach($text as $user) {
list($u) = explode('^', $user);
echo "<tr>" . "<td>" . $u . "</td>" . "</tr>\n";
}
?>
</tr>
<tr>
<th><br></th>
</tr>
<tr>
<th>Total</th>
</tr>
</table>
</body>
</html>
最佳答案
请不要这样做。如果你想使用文本文件(对于一个小的应用程序来说是可以的)然后使用一个合理的文本格式。取消所有这些自定义分隔符,只使用 CSV 格式(您可以使用 fgetcsv() 将一行读入数组),或者更好的是,使用 JSON使用适当的标签格式化(使用 json_decode() 解压它),您在提取时间上的麻烦就会烟消云散。
您也不需要所有那些胡乱提取日期的东西 - 查找 DateTime::createFromFormat()让自己的生活变得轻松。
关于PHP 使用 explode() 读取文本文件以获取文本的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777551/