有谁知道如何在这个文本文件中进行测试
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
$str = '2013-04-11^12:00|4:00|14:00';
$nstr = explode('^',$str);
$nnstr = explode('|',$nstr[1]);
$nnnstr = array_map('trim',$nnstr);
echo '<pre>';
print_r($nnnstr);
键盘链接 - http://codepad.org/Kjgpgh5x