2012-03-30 2 views
0

내 파일은 다음과 같습니다. http://www.mediafire.com/?17bggsa47u4ukmx 기본적으로 다른 확장자를 가진 텍스트 파일입니다.javascript/php에서 텍스트 파일을 구문 분석 하시겠습니까?

이것은 html/javascript/php 용 테스트 리더를 작성하는 테스트입니다. 첫 번째 질문을 의미하는 $$ 0을 읽은 다음 그 다음 줄의 내용을 얻고 싶습니다. 항상 다음 질문이됩니다.

내 코드 : PHP에서 : 파일의

$lines = file("hlm08.dat"); //file in to an array 
// Make it search the whole file possible using a while loop by replacing the $$0 
if (in_array("$$0", $lines)) { 
    echo "I found first Question Number"; 
    /// Code to find the question right after it 
} 

부 :

Hotel and Lodging Management 
I 
$$0 

What do some hotel chains develop to establish formal relationships with employees? 

A. Applications 
B. Regulations 
C. Policies 
D. Contracts 


/D 
Contracts. Contracts are agreements between two or more people or organizations 
stating that one party is to do something in return for something provided by 
the other party. Employment contracts usually specify what an employee is 
expected to do in exchange for being compensated by the hotel chain. These 
contracts are often considered legal agreements that establish formal 
relationships with employees. Hotel chains often develop regulations and 
policies that employees are expected to follow, but these do not establish 
formal relationships with the employees. Applications are forms that potential 
employees fill out to apply for jobs. 
$$1 

An impact of antitrust legislation on business is that it prevents hospitality 
businesses from 

A. experiencing growth. 
B. being competitive. 
C. raising prices. 
D. forming monopolies. 

나는 그것이 전체 파일을 검색하고 배열에있는 모든 질문을 넣어하는 방법을 모르겠습니다. 질문은 각 $$ 0 (질문 번호) 바로 뒤에옵니다.

답변

0

Array (
    [0] => First question 
    [1] => Second question 
    [2] => Third question 
    . 
    . 
    . 
etc 
) 
+0

는'line' 더 이상 foreach 루프 내부 배열입니다 (동일한 서버에 텍스트 파일을 가정) . – Starx

+0

@Starx 죄송합니다! 죄송합니다. – slash197

+0

이 코드를 시도했지만 작동하지 않습니다. 나는 내 코드에서 제거한 여분의 괄호를 발견했다. 여전히 효과가 없었습니다. –

0

내가 foreach는 당신이하려고하는 일을 할 수 있다고 생각 $ 배열이

$array = array(); 

$lines = file("hlm08.dat"); //file in to an array 
foreach ($lines as $line) 
{ 
    if (stripos($line, "$$") !== false) 
    { 
     $array[] = str_replace("$$", "", $line)); 
    } 
} 

출력을 시도

$lines = file('hlm08.dat'); 

foreach ($lines as $line_num => $line) { 
    if($line == '$$0') { 
     echo $lines[$line_num+1]; //'get the next line 
    } 
} 

업데이트 :

하지만 업데이트 한 후에는 정규식을 사용하여 콘텐츠를 추출하는 것이 더 좋을 수도 있습니다.

$text = file_get_contents('hlm.dat'); 
preg_match_all('/\$\$[0-9](.*?)\$\$/', $text, $matches); 
print_r($matches); 
1

귀하의 상황에 가장 적합한 기능은 explode()입니다. 파일의 질문은 여러 줄로 구성되어 있기 때문입니다. 하나 개

$file_content = file_get_contents("hlm08.dat"); 
$questions = explode("\n$$", $file_content); 

print_r($questions); 
0

자바 스크립트 버전 루프 라인 하나에 모든 질문을 결정할 그래서 하드

<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $('<div />').load("hotels.txt",function() { 
    var texts = $(this).text().split("$$"); 
    $.each(texts,function(i, data) { 
     var question = $.trim(data).split('?')[0]; 
     $("#question").append(question+'<hr/>')  
    }); 
    }); 
}); 
</script> 
</head> 
<body> 
<div id="question"></div> 
</body> 
</html>