簡體   English   中英

PHP獲取文本文件中間

[英]PHP get middle of text file

有誰知道如何在此文本文件中進行測試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM