包含.txt内容WITHOUT服务器端lang? [英] Include .txt content WITHOUT server side lang?
问题描述
我有一些数据存储在文本文件中,值分隔如下:Name1 | Number1 | Name2 | Number2 |
我使用php调用一些单独的值,如下所示:
<?php
$ fp = fopen('db.txt','r');
if(!$ fp){echo'ERROR';退出;}
$ loop = 0;
while(!feof($ fp)){
$ loop ++;
$ line = fgets($ fp,1024); //使用2048,如果行很长
$ field [$ loop] = explode('|',$ line);
?>
< p>一些文本..<?php echo''。$ field [$ loop] [2]。''; ?>< / p为H.
< p>更多文字。文本和值<?php echo''。$ field [$ loop] [4]。''; ?>< / p为H.
<?php
$ fp ++; }
fclose($ fp);
?>
现在我需要构建完全相同的站点,从同一个数据库(.txt文件)但我无法使用任何PHP或服务器端lang。有没有办法做到这一点没有服务器端,这样我可以保存我的文件为.html?是否有可能包含一个文本文件(并调用分隔值)与JavaScript?
发现这样的,但我不知道如何分开值并且一次只调用/获取一个值?现在整个文本文件显示在跨度中。如果你能做类似于php代码的东西,那会很好。
< script>
jQuery.get('db.txt',function(data){
$('#text')。html(data.replace('|','< br />') );
});
< / script>
< span id = 文本 >< /文本>
你能帮我一下吗? :S
更新:我的回答
这是我得到的,认为它运作得很好,或者你有什么建议如何改进它?
< script type =text / javascript>
jQuery.get('db.txt',function(data){
$('#db')。html(data);
var element = $(#db) text(element [1]);
$('#3')。text(element [3]).text()。split(|);
$('#1' );
});
< / script>
< span id =dbstyle =display:none;>< / span>
< span id =1>< / span>
< span id =3>< / span>
如果我的文本文件看起来像这样Name1 | Number1 | Name2 | Number2 |它会显示:
Number 1
Number 2
<看看这里的说明:
关于如何使用客户端语言(如Javascript)动态加载内容。
这里还有一个问题:
显示如何加载数据放入一个变量中做一些处理和解析。
I have some data stored in a text file, with values separated like this: Name1 | Number1 | Name2 | Number2 |
I call some separate values with php like this:
<?php
$fp = fopen('db.txt','r');
if (!$fp) {echo 'ERROR'; exit;}
$loop = 0;
while (!feof($fp)) {
$loop++;
$line = fgets($fp, 1024); //use 2048 if very long lines
$field[$loop] = explode ('|', $line);
?>
<p>Some text.. <?php echo ''.$field[$loop][2].''; ?></p>
<p>Some more text. Text and value <?php echo ''.$field[$loop][4].''; ?></p>
<?php
$fp++; }
fclose($fp);
?>
Now I need build the exact same site, loaded from the same database (the .txt-file) but I can't use any php or server side lang at all. Is there any way to do this without server side so that I can save my files as .html? Is it possible to include a text file (and call separated values) with javascript?
Found something like this, but I don't know how to separate the values and only call/get one value at a time? Right now the whole text file is displayed in the span. Would be nice if you could do something similar to the php code.
<script> jQuery.get('db.txt', function(data) { $('#text').html(data.replace('|','<br />')); }); </script>
<span id="text"></text>
Can you help me out here? :S
UPDATE: My answer
This is what I've got, think it works pretty well, or do you have any suggestions how to improve it?
<script type="text/javascript">
jQuery.get('db.txt', function(data) {
$('#db').html(data);
var element = $("#db").text().split("|");
$('#1').text(element[1]);
$('#3').text(element[3]);
});
</script>
<span id="db" style="display:none;"></span>
<span id="1"></span>
<span id="3"></span>
If my text file looks like this Name1 | Number1 | Name2 | Number2 | it will display:
Number 1 Number 2
Look at the instructions here:
On how you can use client side language such as Javascript to load content dynamically.
There is also another question here:
loading contents of variable in div/modal box
Showing how you can load that data into a variable to do some processing and parsing.
这篇关于包含.txt内容WITHOUT服务器端lang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!