Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript/php中解析文本文件?_Php_Javascript_Html_File Io - Fatal编程技术网

在javascript/php中解析文本文件?

在javascript/php中解析文本文件?,php,javascript,html,file-io,Php,Javascript,Html,File Io,这是我的档案: 它基本上是一个具有不同扩展名的文本文件 这是一个测试,我正在用html/javascript/php构建一个测试阅读器。 我想让它读一下$$0,意思是第一个问题,然后再看下一行内容,这永远是一个问题 我的代码: 在php中: 文件的一部分: Hotel and Lodging Management I $$0 What do some hotel chains develop to establish formal relationships with employees?

这是我的档案: 它基本上是一个具有不同扩展名的文本文件

这是一个测试,我正在用html/javascript/php构建一个测试阅读器。 我想让它读一下$$0,意思是第一个问题,然后再看下一行内容,这永远是一个问题

我的代码: 在php中:

文件的一部分:

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(问题数量)后面都有问题

试试这个

$array = array();

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

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

我想foreach可以做你想做的事

$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);

我认为就你的情况而言,最好的功能是。因为文件中的问题由多行组成。所以很难用一条接一条的循环线来确定整个问题

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

print_r($questions);
Javascript版本(假定文本文件位于同一服务器上)


$(文档).ready(函数(){
$('').load(“hotels.txt”,函数(){
var text=$(this.text().split($);
$。每个(文本、函数(i、数据){
变量问题=$.trim(数据).split('?')[0];
$(“#问题”).append(问题+”
) }); }); });
不再是foreach循环中的数组。我尝试了此代码,但它不起作用。我在代码中发现了一个额外的括号,我也删除了它。它仍然不起作用。我知道,在我回答问题后添加的示例文件中,它永远不会起作用。等等,如何在包含文本的数组中搜索下一行。比如跳过那些没有文本或任何内容的内容。
if($line==“”){…}
。你有PHP知识吗?
$file_content = file_get_contents("hlm08.dat");
$questions = explode("\n$$", $file_content);

print_r($questions);
<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>