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

标签 php css html

所以我有两个文本文件,一个叫做 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/

相关文章:

html - 在CSS中水平拉伸(stretch)背景图像

html - 使用 CSS 裁剪图像的所需部分

php - WooCommerce 如何检查产品是否有货

php - 从 php 脚本显示虚拟主机?

javascript - 通过 jQuery 在 textarea 占位符内换行

javascript - 如何根据 Material UI (React JS) 中的要求制作一个 'Select' 组件

javascript - 使用或单击输入时不允许拖动

javascript - 如何在 div rel 中插入 HTML?

javascript - javascript中的Laravel Eloquent 关系问题

html - 如何控制图片的可点击区域? CSS