2012-03-30 20 views
0

Hier ist meine Datei: http://www.mediafire.com/?17bggsa47u4ukmx Es ist im Grunde eine Textdatei mit einer anderen Erweiterung.Parsing Textdateien in Javascript/PHP?

Es ist ein Test, dass ich einen Testleser für in html/javascript/php baue. Ich möchte, dass es das $$ 0 liest, was die erste Frage bedeutet, und dann die nächste Zeile des Inhalts danach bekommen, was immer eine Frage sein wird.

Mein Code: in 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 
} 

Teil des Dossiers:

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. 

Ich bin nicht sicher, wie es die gesamte Datei zu machen scannen und alle Fragen in einer Reihe gelegt. Die Fragen kommen direkt nach jedem $$ 0 (Nummer der Frage).

Antwort

0

Versuchen Sie, diese

$array = array(); 

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

Ausgabe von $ array

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

'line' ist nicht mehr ein Array innerhalb der foreach-Schleife, um zu bestimmen . – Starx

+0

@Starx Hoppla! Es tut uns leid. – slash197

+0

Ich habe diesen Code versucht, aber es funktioniert nicht. Ich habe eine zusätzliche Klammer gefunden, die ich auch in meinem Code entfernt habe. Es hat immer noch nicht funktioniert. –

0

nehme ich foreach tun können, was Sie

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

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

aktualisieren versuchen:

Aber nach dem Update, extrahieren Sie den Inhalt mit Regex möglicherweise besser.

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

ich für Ihre Situation denken, ist die beste Funktion explode(). Weil die Fragen in der Datei aus mehreren Zeilen bestehen. So ist es schwer ganze Frage mit Looping Linien nacheinander

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

print_r($questions); 
0

Javascript-Version (übernimmt die Textdatei auf dem gleichen Server)

<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> 
Verwandte Themen