2012-03-30 6 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 
    } 
} 

更新:

しかし、更新した後、regexを使ってコンテンツを抽出する方が良いかもしれません。

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

あなたの状況では、最高の機能はexplode()だと思います。ファイル内の質問は複数の行で構成されているためです。だから、そのループ線で全体の問題を決定するのは難しい1

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

print_r($questions); 
ずつ
0

Javascriptのバージョン(同じサーバー上のテキストファイルを想定)

<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> 
関連する問題