谁知道如何在这个文本文件中进行测试
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/