用于从文件中获取整数的标记化字符串


Tokenized strings to get integers from a file

我有一个文件.dat,它的格式是

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

这是我试图获得正确整数的php代码,例如在第一行中,我只想获得值";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];
}

如果我使用reading[0],结果只是"1"

reading[1]上,它给出错误

"SCREAM:忽略(!)的错误抑制

注意:未初始化的字符串偏移量:C:''wamp''www''Delta Compression ''MaxLength.php中第16行的1;

使用正则表达式会更快

$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

$Matches将是

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
        )
)

代码与代码:

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 

您没有正确使用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];

我认为您应该研究file()和explode()。File()会为您将文件的每一行读取到一个数组中,然后您可以使用explode()来表示空格和小数点。

正如其他答案所建议的那样,您可以使用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, ' '));

或者您可以使用正则表达式,如果您熟悉regex:,这可能是您最简单的选择

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

正在分解正则表达式。。。

  • (-打开组(内容进入$matches[0]
  • 'd+-匹配一个或多个数字
  • )-关闭捕获组
  • (-打开另一个组(我们将使该组成为可选组)
  • '.-匹配文字.
  • 'd+-匹配一个或多个数字
  • )-关闭捕获组
  • ?-使前面的组是可选的(如果需要,这允许像1303100650 115这样的字符串)
  • $-匹配字符串末尾

这些例子只针对一个字符串。显然,您希望在循环中完成此操作(或者只使用preg_match_all)。