2013-07-25 87 views
0

我有一個FILE.DAT得到整數,它的格式是記號化字符串從文件

1303100643 115.83 
1303100644 115.94 
1303100645 115.80 
1303100646 115.99 
1303100647 115.74 
1303100648 115.11 

這裏就是我試圖得到我會第一線例如右整的PHP代碼想如果我使用reading[0]結果只得到值「115」

while (!feof($file_handle)) { 
    set_time_limit(0); 
    $line_of_text = fgets($file_handle, 1024); 
    $reading=strtok($line_of_text[0]," "); 
    echo $reading[0]; 
} 

只是"1"

reading[1]提示錯誤

「SCREAM:錯誤抑制被忽略(! )

注意:未初始化字符串偏移量:1 C:\ WAMP \ WWW \增量壓縮\ MaxLength.php第16" 行

回答

1

您沒有使用strtok()適當strtok()被初始化,然後每個。後續調用給你的下一個令牌所以$reading[0]是真正拉動字符串的第一個字符

您使用strtok()explode(),所以只需使用explode():。

while (!feof($file_handle)) { 
    set_time_limit(0); 
    $line_of_text = fgets($file_handle, 1024); 
    $reading=explode(" ", $line_of_text[0]); 
    echo $reading[0]; 
} 

我想只得到值 「115」

你可以簡單地將結果強制轉換爲int或使用int_val()

echo (int)$reading[1]; 
+0

你是一個救生員:D – Waqas

1

我想你應該看看到文件()和爆炸()。 File()會將文件的每一行讀取到一個數組中,然後可以使用explode()作爲空格和小數點。

2

使用正則表達式會更快

$data = file_get_contents("file.txt"); 
preg_match_all("/([0-9]{10}) ([0-9]{3}\.[0-9]{2})/",$data,$Matches); 

//Use below if you want an associative array with the first 10 numbers 
//being the keys and the second numbers being the values 
$myData = array_combine($Matches[1],$Matches[2]); 

([0-9]{10})匹配的前10個數字鍵0-9,

([0-9]{3}\.[0-9]{2})匹配的下一組具有3號0-9然後過一段然後2號更多的數字0-9

$比賽將是

Array 
(
    [0] => Array 
     (
      [0] => 1303100643 115.83 
      [1] => 1303100644 115.94 
      [2] => 1303100645 115.80 
      [3] => 1303100646 115.99 
      [4] => 1303100647 115.74 
      [5] => 1303100648 115.11 
     ) 

    [1] => Array 
     (
      [0] => 1303100643 
      [1] => 1303100644 
      [2] => 1303100645 
      [3] => 1303100646 
      [4] => 1303100647 
      [5] => 1303100648 
     ) 

    [2] => Array 
     (
      [0] => 115.83 
      [1] => 115.94 
      [2] => 115.80 
      [3] => 115.99 
      [4] => 115.74 
      [5] => 115.11 
     ) 

) 

代碼VS代碼:

JasonMcCreary

$time1=microtime(); 
$mydata = array(); 
$file_handle = fopen("data.txt","r"); 

while (!feof($file_handle)) { 
    set_time_limit(0); 
    $line_of_text = fgets($file_handle, 1024); 
    $reading=explode(" ", $line_of_text); 

    $mydata[] = $reading; 
} 
fclose($file_handle); 
$time2 =microtime(); 

讀一行一行並使用爆炸

1374728889 0.20137600 :: 1374728889 0.20508800 
0.20508800 
0.20137600 
---------- 
0.00371200 

$time1=microtime(); 

$data = file_get_contents("data.txt"); 
preg_match_all("/([0-9]{10}) ([0-9]{3}\.[0-9]{2})/",$data,$Matches); 
$myData = array_combine($Matches[1],$Matches[2]); 

$time2=microtime(); 

echo $time1." :: ".$time2; 

使用FGC和正則表達式

1374728889 0.20510100 :: 1374728889 0.20709000 
0.20709000 
0.20510100 
---------- 
0.00198900 
+0

雖然它有效,但我不確定正則表達式是否適合工作。 –

+0

@JasonMcCreary,怎麼樣?更少的代碼,更清楚發生了什麼,他可以簡單地使用'array_combine'來創建一個關聯數組:$ myData = array_combine($ Matches [1],$ Matches [2]);'似乎是正確的工具。 –

+0

如果你對正則表達式感到滿意,假定輸入可靠一致(看起來是這樣),那麼正則表達式就是**完美**工具。少打字總是很好。 – sgroves

1

你可以使用explode,其他答案建議,或者你可以得到的空間和小數點的位置,並使用substr,讓他們之間的字符。假設你的輸入是一致的,無論是strposstrrpos將這項工作:

$line = '1303100643 115.83'; 

$space_pos = strrpos($line, ' '); 
$decimal_pos = strrpos($line, '.'); 

$number = substr($line, $space_pos, $space_pos + count($line) - $decimal_pos); 

另一種方法是在空間後得到的一切,然後乘其地板或將其轉換爲整數。幸運的是,你可以在一個簡單的做到這一點使用相同的功能,前面的例子來讀取一個班輪:

$number = (int)substr($line, strrpos($line, ' ')); 

或者你可以使用正則表達式,這可能是你最簡單的選擇在這裏如果你'熟悉正則表達式:

if (preg_match('|(\d+)(\.\d+)?$|', $line, $matches)) { 
    $number = $matches[0]; 
} 

打破那個正則表達式...

  • ( - 公開組(內容進入$matches[0]
  • \d+ - 匹配一個或多個數字
  • ) - 關閉捕獲組
  • ( - 打開另一組(我們將讓這組可選)
  • \. - 匹配文字.
  • \d+ - 匹配一個或多個數字
  • ) - 靠近捕獲組
  • ? - 使前述組可選的(這允許琴絃等1303100650 115,如果需要的話)
  • $ - 串的匹配端

這些示例僅適用於一個字符串。很明顯,你會想要在循環中做到這一點(或只使用preg_match_all)。