0

Here is my file: http://www.mediafire.com/?17bggsa47u4ukmx It is basically a text file with a different extension.

It is a test that i am building a test reader for in html/javascript/php. I want it to read the $$0 meaning the first question and then get the next line of content after it, which will always be a question.

My 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
}

Part of file:

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.

I am not sure how to make it scan the whole file and put all the questions in an array. the questions come right after each $$0 (number of the question).

0

4 Answers 4

1

I think for your situation , the best function is explode(). Because the questions in the file consist of multiple lines. So its hard to determine whole question with looping lines one by one

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

print_r($questions);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

$array = array();

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

Output of $array

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

5 Comments

line is no longer an array inside the foreach loop.
i tried this code, but it isn't working. I found an extra parenthesis that i removed as well in my code. It still didn't work.
I know, it will never work with the sample file you added after I answered the question.
wait, how do i search for the next line in an array that has text. Like skip the ones that don't have text or anything in them.
if ($line == "") { ... }. Do you have any PHP knowledge?
0

I suppose foreach can do what you are trying

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

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

Update:

But after you update, extract the content using regex might be better.

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

Comments

0

Javascript version (assumes the text file on the same 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.