【发布时间】:2014-02-28 18:25:21
【问题描述】:
我对以下 php 脚本是否可以用 Javascript(或 HTML5)编写感兴趣?基本上这只是一个读取 .txt 并从中显示随机行的简单文件:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<html>
<body>
<h2><?php echo $randPhrase; ?></h2>
</body>
</html>
“flatFileDB.txt”示例:
Line 1
Line 2
Line 3
Line 4
Line 5
...
...
etc
示例取自这里:http://www.developphp.com/view.php?tid=1213,我也想这样做,但没有服务器端 php。有没有可能?
【问题讨论】:
-
有可能吗?是的
-
好的,既然我们已经确定这是可能的,那么让我们来寻找解决方案。
-
Ajax 调用或文件 api,获取文本,在新行上拆分字符串,生成随机数,从数组中选择索引。玩得开心编码。
标签: javascript php html random flat-file