2014-12-28 2 views
0

많은 데이터가 포함 된 텍스트 파일이 있습니다. 나는 성명 사이에서 필드 이름을 추출하는 것으로까지 생각해 봤다.PHP를 사용하여 TEXT 파일에서 반복 문자열 찾기

내 코드에서 수행하고자하는 작업은 전체 텍스트 파일을 스캔하는 것입니다. 웬일인지 문자/문자열의 첫 번째 발생으로 중지됩니다. "16349", "카테고리" "모험", "이름": 나는 반환

"CAT_ID"가지고

<?PHP 

//First, open the file. Change your filename 
$file = "datafile1.txt"; 
$handle = fopen($file, "r"); 
$contents = fread($handle, filesize($file)); 

for ($i=0; $i=100; $i+10){ 
    $word1='"cat_id'; 
    $word2='"category"'; 

    $a = strpos($contents, $word1); 
    $b = strpos($contents, $word2); 

    $between=substr($contents, $a, ($b - $a)); 

    echo $between; 

    ////////////////////////////////// 

    $word1='"category'; 
    $word2='"name"'; 

    $c = strpos($contents, $word1); 
    $d = strpos($contents, $word2); 

    $between=substr($contents, $c, ($d - $c)); 

    echo $between; 
    //////////////////////////////////// 

    $word1='"name'; 
    $word2='"description"'; 

    $e = strpos($contents, $word1); 
    $f = strpos($contents, $word2); 

    $between=substr($contents, $e, ($f - $e)); 

    echo $between; 
} 
fclose($handle); 

?> 

"어쌔신의 신조 IV 블랙 플래그 - X 박스 360",

하지만 cat_id와 카테고리가 반복되는 곳에서 멈추고 컴퓨터 게임의 이름도 잘됩니다.

전체 텍스트 파일을 스캔해야 검색이 반복되므로 잘하면 게임 및 카테고리 출력 결과를 얻을 수 있습니다.

* 편집 : 죄송합니다. 다음은 파싱이 필요한 데이터 파일의 샘플입니다.

"cat_id": "16349", 
    "category": "Adventure", 
    "name": "Assassin's Creed IV Black Flag - Xbox 360", 
    "description": "It is 1715. Pirates rule the Caribbean and have es... (visit site URLs for full  description)", 
    "updated_at": 1419672679, 
    "width": "139.70", 
    "sem3_id": "1AEIvknN7uwqG2GcwSCMK8", 
    "created_at": 1374830955, 
    "platform": "Xbox 360", 
    "height": "12.70", 
    "length": "190.50", 
    "sitedetails": [ 
    { 
     "sku": "B00BMFIXT2", 
     "latestoffers": [ 
     { 
      "seller": "JNJ Shop", 
      "lastrecorded_at": 1419672600, 
      "currency": "USD", 
      "firstrecorded_at": 1419672600, 
      "id": "7g2fpY7BOSE0sU2oKkUkeY", 
      "price": "11.00", 
      "shipping": "3.99", 
      "condition": "New" 
     }, 

200 lines later..... 

"cat_id": "20923", 
    "category": "Games", 
    "name": "Disney Infinity Starter Pack - PlayStation 3", 
    "description": "Product Description Platform: PlayStation 3 | Edit... (visit site URLs for full       description)", 
    "updated_at": 1419563879, 
    "width": "269.24", 
    "created_at": 1375817329, 
    "sem3_id": "0FIqEyeRf4SMgiYaoKC6yO", 
    "platform": "PlayStation 3", 
    "height": "90.93", 
    "length": "358.39", 
    "sitedetails": [ 
    { 
     "sku": "7635065", 
     "latestoffers": [ 
     { 
      "seller": "BestBuy", 
      "lastrecorded_at": 1419552600, 
      "firstrecorded_at": 1419015000, 
      "currency": "USD", 
      "availability": "In stock", 
      "price": "66.98", 
      "id": "5EefaVFIhs2UKYA0Q0qIae", 
      "condition": "New" 
     }, 
+0

텍스트 파일의 항목은 어떻게 든 구분 (예를 들어 새로운 라인과)? 텍스트 파일의 일부를 복사/붙여 넣기 할 수 있습니까? – sinisake

답변

0

a 실제로 중지되지 않습니다. 동일한 콘텐츠를 피드 할 때마다 http://php.net/manual/en/function.strpos.php에 따라 지정된 텍스트와 동일한 항목이 표시됩니다.

다음 반복에서 시작할 위치를 지적하려면 세 번째 매개 변수 [, int $ offset = 0]를 사용해야 할 수도 있습니다. Smth. like :

$a = 0; 
$b = 0; 

for ($i=0; $i=100; $i+10){ 
    $word1='"cat_id'; 
    $word2='"category"'; 

    $a = strpos($contents, $word1, $a); 
    $b = strpos($contents, $word2, $b); 

동일한 단어 "cat_id"및 "category"를 사용하려는 경우 반복 외부에서 초기화하십시오.

더 잘 사용하는 거라고 모든 항목을 잡기위한 "동안"사이클 :

$catWord = '"cat_id"'; 
$categoryWord = '"category"'; 

$a = 0; 
$b = 0; 
while (($a = strpos($content, $catWord, $a)) !== false) { 
    $b = strpos($content, $categoryWord, $b); 

    $between = .... 
관련 문제