PHP获取文本文件的中间部分

标签 php css html

谁知道如何在这个文本文件中进行测试

2013-04-11^12:00|4:00|14:00

2013-07-21^10:00|15:00|18:00|15:00

2013-12-11^13:00|14:00

我想获取 12:00 的值 4:00 14:00 10:00 等...忘记其余的

我尝试使用 explode() 执行此操作,但它不起作用,而且我无法使用子字符串,因为我始终不知道该位置..

list($year, $month, $day) = explode('-', $s); 

最佳答案

你的意思是:

    $str = "2013-04-11^12:00|4:00|14:00";
    list($year, $rest) = explode("^", $str);
    $rest_arr = explode("|", $rest);
    echo "<pre>"; print_r($rest_arr);
    //gives
    Array
    (
      [0] => 12:00
      [1] => 4:00
      [2] => 14:00
    )

或者如果你想从 txt 文件中做到这一点:

//get the contents in array
$str = file("your_file.txt");
foreach($str as $val) {
    //$val is string like '2013-04-11^12:00|4:00|14:00'
    list($year, $rest) = explode("^", $val);
    $rest_arr = explode("|", $rest);
    echo "<pre>"; print_r($rest_arr);
    //or echo it
    foreach($rest_arr as $time) {
        echo $time."<br />";
    }
}

关于PHP获取文本文件的中间部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18778167/

相关文章:

html - 如何让<div>填充<td>高度

javascript - 有什么方法可以使用纯 CSS 在单击时隐藏复选框父 div?

php - Laravel 5 文件删除错误

javascript - php "||"赋值逻辑与Javascript对比

jquery - 避免 html 表格在鼠标悬停时闪烁

javascript - 我如何将此代码中的选项卡居中?

php - 如何使用PHP显示特定列的最后插入行的数据

php - 交响乐 4 : Namespace issue when generating entities with doctrine

javascript - 需要在 (document).ready() 之前填充 Javascript 数组

javascript - 单击按钮时将值存储在 JSON 中