2014-11-15 3 views
0

strpos()가 얻은 위치에서 전체 라인을 얻으려면 어떻게해야합니까?PHP - 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"를 지역화하고, 자신의 전체 라인을 읽고, 일부 데이터를 에코. 모든 종류의 도움을 환영합니다!

+0

왜 모든 반복에 대한 파일 열기? –

+1

우선,'file_get_contents()'를 루프 밖으로 가져와야합니다. 거기에 당신이 가지고있는 방식으로 각 루프 반복마다 파일을로드하고 있습니다. – JakeParis

+0

이미 삭제되었습니다. 죄송합니다. –

답변

1

면책 조항 : VIKIK은 $ 내용의 NAXOP 앞에옵니다. VIKIKNAXOP || 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; 
    } 
} 

다음은 더 나은 프로그램입니다. 여기

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

는 '여기에 TEST.DAT

NAXOP 
UZ45 
VIKIK 
UZ45 VIKIK 
NAXOP 
SILLYSTRING 

입니다

NAXOP 
UZ45 
VIKIK 
UZ45 VIKIK 
NAXOP 
+0

첫 번째 접근 방식은 완벽합니다. 감사합니다! –

0

내가 한혜진 많은 프로그램이 라이브러리를 사용했습니다 출력 .. 그것을 확인 내가 생각을하다 당신이 가진 문제를 정확히 풀 수 있습니다. 그것은 .. 정확히 말씀처럼 작동

예컨대

between ('@', '.', '[email protected]'); 
//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); 
}; 
?> 
관련 문제