PHP使用explode()读取文本文件以获取文本的特定部分

PHP使用explode()读取文本文件以获取文本的特定部分,php,css,html,Php,Css,Html,所以我有两个文本文件,一个叫做users.txt和schedule.txt。schedule.txt如下所示:(注意,上面的数字不在文件中,它们用作用户文件的引用) 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文件如下所示: 约翰·史

所以我有两个文本文件,一个叫做users.txt和schedule.txt。schedule.txt如下所示:(注意,上面的数字不在文件中,它们用作用户文件的引用)

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文件如下所示:

约翰·史密斯

星球大战^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格式(您可以使用CSV格式将行读入数组),或者更好地使用带有适当标记的格式(用于解包),提取时间的麻烦就会消失


你也不需要所有那些胡说八道的提取日期——查找并让自己的生活变得轻松。

上面的人已经对存储信息的更好方法发表了评论,但要回答你关于提取()的问题:

当你这样做的时候

list($year, $month, $day) = explode('-', "2013-04-11^12:00|4:00|14:00"); 
对于
$day
的值,您将获得
“11^12:00 | 4:00 | 14:00”
,而不是
“11”
。同样,当你这样做的时候

$ga = explode("|", "2013-04-11^12:00|4:00|14:00");

第一个值是
“2013-04-11^12:00”
。从代码的其余部分来看,这可能不是您想要的方式。

我为您编写了一个小类。我没有包括任何错误检查。对于
^
之后的第一次仍然有点困惑,因此我在代码中添加了关于如何包含它的注释。还请注意,我没有评论的代码,希望你能理解它

class UserTime{
    var $data=array();
    function __construct(){
        $lines = file('test.txt', FILE_IGNORE_NEW_LINES);
        foreach ($lines as $l){
            list($d,$t)=explode('^', $l);
            $this->data[]=$d;
            $t=explode('|', $t);
            // you have to remove the following line if you want to
            // include the first time after ^
            array_shift($t);
            $this->data = array_merge($this->data,$t);
        }
    }
    function getUser($user_string){
        list($user_name,$ix)=explode('^', $user_string);
        $userData['userName']=$user_name;
        $userData['time']=array();
        $idx = explode('|', $ix);
        for($i=0;$i<count($idx);$i++){
            if(isset($this->data[$idx[$i]])){
                $userData['time'][]=$this->data[$idx[$i]];
            }
        }
        return $userData;
    }
}
输出如下。我想你们可以在你们的HTML中自己使用它

Array
(
    [userName] => John Smith
    [time] => Array
        (
            [0] => 14:00
            [1] => 15:00
            [2] => 15:00
        )

)

你的两张桌子之间的关系还很不清楚。约翰·史密斯如何从4的索引中得到15:00的时间?我就是看不出来。我可以建议您在尝试编写代码之前简化此操作并自己处理它吗?您越早切换到RDBMS,您的
schedule.txt
越大?它能存储在内存中吗?我也支持@Dagon的建议
15:00(从4点开始)
对不起,伙计们。。我这样做是为了一个要求。。。必须使用两个文本文件。假设schedule.txt文件已经存在,我已经列出了它。那个家伙清楚地说他是一个新手。。。你能详细说明什么是“合理的文本格式”吗?我不太明白所有这些是如何工作的,以及你到底想要什么输出。谢谢你的努力!我试图做的是在左侧有一个包含所有用户的表,并将时间作为标题放在顶部。当用户文件上有一个数字时,它将在包含计划时间的框中打一个复选标记。。当我为用户添加一个编辑按钮,以便他们可以选择表块(使用单选按钮)写入文件,然后将计划文件拉回到中时,这一点最终会发生改变。。。希望天气转晴
$users = new UserTime();
$udata = $users->getUser('John Smith^2|4|6');
print_r($udata);
$udata = $users->getUser('Star Wars^0|1|3|6');
print_r($udata);
Array
(
    [userName] => John Smith
    [time] => Array
        (
            [0] => 14:00
            [1] => 15:00
            [2] => 15:00
        )

)