PHP - 从一个位置读取完整行,strpos()

时间:2014-11-15 02:52:24

标签: php strpos

如何从strpos()获得的位置获得完整的一行?

这是我的代码:

//$route_route sample: NAXOP UZ45 VIKIK

$route_waypoint = explode(" ", $route_route);
$file = "components/airways.txt";
$contents = file_get_contents($file);

foreach ($route_waypoint as $waypoint) {

   //$waypoint sample: NAXOP    
   $pos = strpos($contents, $waypoint);

      if ($pos === false) {

         continue;

      } else {

      $line = fgets($contents, $pos); // Here is my problem

      //line sample: MES,W714,005,NAXOP,38.804139,30.546833,149,000, ,L

      list($fix, $lat, $lon, $sample_data1, $sample_data2) = explode(',', $line);

      echo "$fix, $lat, $lon";     

   }

}

主要目标是本地化" NAXOP",阅读他的完整行,并回显一些数据。欢迎任何形式的帮助!

2 个答案:

答案 0 :(得分:1)

免责声明:如果VIKIK位于$ {1}}之前。 NAXOP将无法找到,如果多次出现VIKIK,则只会找到每次出现的第一个。

NAXOP || UZ45 || VIKIK

这是一个更好的计划。

$len = strlen($contents);
$pos = 0;
foreach ($route_waypoint as $waypoint) {
   $pos = strpos($contents, $waypoint, $pos);
   if ($pos === false) {
       continue;
   } 
   else {
       // Here is the critical section for matching the entire line:

       // First look backwards from where $waypoint was found for the 
       // beginning of the line
       $startOfLine = strrpos($contents, "\n", ($pos - $len));

       // Next look forwards from $waypoint to find the end of the line
       $endOfLine = strpos($contents, "\n", $pos);

       // we already have the file in memory, just read from that,
       $line = substr($contents, $startOfLine, ($endOfLine - $startOfLine));

       list($fix, $lat, $lon, $sample_data1, $sample_data2) 
                      = explode(',', trim($line));

       echo "$fix, $lat, $lon";

       // IDK if you want to match the same line twice or not.
       $pos = $endOfLine;
   }
}

这是test.dat

<?php 

$str = "NAXOP UZ45 VIKIK";
$regex = "/" . preg_replace("/ /", "|", $str) . "/";

$fp = fopen("test.dat", "r");

while ($line = fgets($fp)) {
    if (preg_match($regex, $line)) {
        echo $line; 
    }
}
fclose($fp);

这是输出

NAXOP
UZ45
VIKIK
UZ45 VIKIK
NAXOP
SILLYSTRING

答案 1 :(得分:0)

我已经在很多程序中使用过这个库了。看看它我认为它会解决你遇到的问题。它的工作方式与它说的完全一样。

e.g

between ('@', '.', 'biohazard@online.ge');
//returns 'online'
//from the first occurrence of '@'

src:http://php.net/manual/en/function.substr.php

    <?php

    function after ($this, $inthat)
    {
        if (!is_bool(strpos($inthat, $this)))
        return substr($inthat, strpos($inthat,$this)+strlen($this));
    };

    function after_last ($this, $inthat)
    {
        if (!is_bool(strrevpos($inthat, $this)))
        return substr($inthat, strrevpos($inthat, $this)+strlen($this));
    };

    function before ($this, $inthat)
    {
        return substr($inthat, 0, strpos($inthat, $this));
    };

    function before_last ($this, $inthat)
    {
        return substr($inthat, 0, strrevpos($inthat, $this));
    };

    function between ($this, $that, $inthat)
    {
        return before ($that, after($this, $inthat));
    };

    function between_last ($this, $that, $inthat)
    {
     return after_last($this, before_last($that, $inthat));
    };

// use strrevpos function in case your php version does not include it
function strrevpos($instr, $needle)
{
    $rev_pos = strpos (strrev($instr), strrev($needle));
    if ($rev_pos===false) return false;
    else return strlen($instr) - $rev_pos - strlen($needle);
};
?>