2013-07-25 45 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)。