将.txt中的特定行加载到html中 [英] Load a certain line from a .txt into html

查看:135
本文介绍了将.txt中的特定行加载到html中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文本文件中的某一行(例如546行)加载到网站中?

How do I load a certain line from a text document (for example line 546) into a website?

推荐答案

我提出了使用旧技术的此解决方案,因为您要求对旧文件格式(html)启用此解决方案!所以就到这里了!不要害怕!我们要做的是使用服务器端包含(SSI)可嵌入公用网关接口(CGI)程序的结果使用"#exec cgi"指令将其转换为静态HTML文档.听起来很复杂,但是非常简单.我们只需要创建两个文件,然后确保正确配置了Web服务器即可.三个简单的步骤!我会逐步指导您:

I propose this solution using old technology because you asked to make this work for an old file format (html)! So here it goes! Don't be scared! What we are going to do is use Server Side Includes (SSI) to embed the results of a Common Gateway Interface (CGI) program into a static HTML document by using the "#exec cgi" directive. It sounds complicated but it's pretty simple. We only have to create two files and then make sure the web server is properly configured. Three simple steps! I'll guide you step by step:

1.创建HTML文件

您的HTML文件内容就是这样(只需使用简单的文本编辑器将其复制并粘贴到服务器上的HTML文件中即可):

Your HTML file content would be just this (simply copy and paste into an HTML file on your server using a simple text editor):

<HTML>
<TITLE>Load a certain line from a .txt into html</TITLE>

Here's the content of line 546 in filename.txt:
<!--#exec cgi="/cgi-bin/perl-print-line.pl 546 filename.txt"-->

</HTML>

2.创建您的PERL脚本文件

上述方法尚无法解决.它取决于一个简单的程序来输出您的行号.它是 perl-print-line. Alvin Alexander的书,您也可以通过将下面的PERL代码复制并粘贴到文本编辑器中来简单地创建此文件(请确保将其放置在cgi-bin目录中):

The above won't work yet. It depends on a simple program to output your line number. It is perl-print-line.pl by Alvin Alexander, and you can simply create this file by copying and pasting the PERL code below into a text editor too (be sure to place it inside your cgi-bin directory):

#!/usr/bin/perl

# purpose: print a specific line from a text file
# usage:   perl-print-line.pl line-number input-file

# use perl argv to verify the number of command line arguments
@ARGV == 2 or die "Usage: print-specific-line.pl line-number input-file\n";
$desired_line_number = $ARGV[0];
$filename = $ARGV[1];

# use perl open function to open the file (or die trying)
open(FILE, $filename) or die "Could not read from $filename, program halting.";

# loop through the file with a perl while loop, stopping when you get to the desired record
$count = 1;
while (<FILE>)
{
  if ($count == $desired_line_number)
  {
    # print line, then get out of here
    print $_;
    close FILE;
    exit;
  }
  $count++;
}
close FILE;

print "Sorry, I think the line number you entered is greater than the number of lines in the file.\n";

3.在Web服务器上配置SSI

现在,除非首先将您的Web服务器配置为处理SSI,否则上述所有操作均将无效.所有流行的Web服务器仍然可以执行此操作,但是您必须首先确保已对其进行配置.根据 PerlScriptsJavaScripts.com 网站

Now, all of the above will not work unless your web server is first configured to process SSI. All popular web servers can still do it, but you must first make sure it is configured. Per the PerlScriptsJavaScripts.com site,

如果您的服务器是Unix/Linux服务器,则可以确定它是 支持SSI. SI要求使用Apache服务器软件.最多 UNIX/Linux服务器使用Apache.某些Windows服务器也使用Apache,但是 他们中的大多数使用IIS(Internet信息服务器).服务器 软件不是您可以更改的东西,因为它可以运行 整个服务器(包括其他人的网站),您的主机也将 拒绝为您更改它.相反,他们可能足够友善 您的站点到另一台使用Apache的服务器上.

If your server is a unix/linux server, you can be pretty sure it supports SSI. SI requires the use of Apache Server software. Most unix/linux server use Apache. Some Windows server also use Apache, but most of them use IIS (Internet Information Server). The server software is not something you can change, and because it runs th entire server (including other people's web sites) your host will also refuse to change it for you. They may instead be kind enough to move your site to anther one of their servers which does use Apache.

大多数人会告诉您要使用SSI,您必须使用 "shtml"扩展名.最初可能是这种情况,但您可以设置 服务器还可以接受并解析常规的"htm"或"html"文件.

Most people will tell you that to use SSI, you must name the page with a "shtml" extension. While that may be the case initially, you can set the server to also accept and parse regular "htm" or "html" files.

如果您使用Windows服务器而不是UNIX/Linux服务器,则可能会找到

If you have a Windows server instead of a UNIX/Linux server, then you might find this article by Robert McMurray useful. Most importantly he states,

默认情况下,在IIS 6上未启用SSI.要在IIS 6上启用SSI,请设置 服务器端包括Web服务扩展的状态 在Internet信息服务(IIS)管理器中允许.

SSI is not enabled by default on IIS 6. To enable SSI on IIS 6, set the status for the Server Side Includes web service extension to Allowed in the Internet Information Services (IIS) Manager.

默认情况下,未在IIS 7上安装SSI.要在IIS 7上安装SSI,请参阅 服务器端包含主题.

SSI is not installed by default on IIS 7. To install SSI on IIS 7, see the Server Side Include topic.

默认情况下,在IIS 5和IIS 6中禁用#exec的cmd指令. 要启用cmd指令,请参见 Microsoft KB文章233969 .

The cmd directive for #exec is disabled by default in IIS 5 and IIS 6. To enable the cmd directive, see Microsoft KB article 233969.

现在已对IIS 7中的SSI文件禁用了#exec的cmd指令; 您只能使用cgi指令.

The cmd directive for #exec is now disabled for SSI files in IIS 7; you can only use the cgi directive.

最后,如果您正在处理IIS 7.5,并且* .html文件中的SSI指令未自动进行预处理,请尝试阅读

Finally, if you are dealing with IIS 7.5 and your SSI directives inside your *.html files aren't being automatically preprocessed, try reading this post.

这篇关于将.txt中的特定行加载到html中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆