从文本文件中的随机行生成 URL?
Generate URL from random row in text file?
我有一个带引号的纯文本文件,其中每个引号单独占一行(仅此而已)。我使用以下代码在网页上显示随机引用。
<?php
$f_contents = file("quote.txt");
$line = $f_contents[array_rand($f_contents)];
echo $line
?>
是否可以为随机报价生成一个 link 以便有人可以再次找到它?
最好的选择可能是 GET 方法。
这意味着当您想要引用 5 时,您可以添加 ?quote=5
作为示例。
但是如果 GET 没有传递任何内容,则显示随机。
if 还检查它是否是在 GET 中传递的数值,并且它不比 txt 文件中的数字(行号)大。
$f_contents = file("quote.txt");
if(!isset($_GET['quote']) || !is_numeric($_GET['quote']) || $_GET['quote'] > count($f_contents)){
$random = array_rand($f_contents);
$line = $f_contents[$random];
echo $line . "<br>\n";
echo "<a href='www.example.com/quote.php?quote=" . $random . ">Link to this quote</a>";
}else{
$line = $f_contents[$_GET['quote']];
echo $line
}
更改锚点 link 以适应!
我有一个带引号的纯文本文件,其中每个引号单独占一行(仅此而已)。我使用以下代码在网页上显示随机引用。
<?php
$f_contents = file("quote.txt");
$line = $f_contents[array_rand($f_contents)];
echo $line
?>
是否可以为随机报价生成一个 link 以便有人可以再次找到它?
最好的选择可能是 GET 方法。
这意味着当您想要引用 5 时,您可以添加 ?quote=5
作为示例。
但是如果 GET 没有传递任何内容,则显示随机。
if 还检查它是否是在 GET 中传递的数值,并且它不比 txt 文件中的数字(行号)大。
$f_contents = file("quote.txt");
if(!isset($_GET['quote']) || !is_numeric($_GET['quote']) || $_GET['quote'] > count($f_contents)){
$random = array_rand($f_contents);
$line = $f_contents[$random];
echo $line . "<br>\n";
echo "<a href='www.example.com/quote.php?quote=" . $random . ">Link to this quote</a>";
}else{
$line = $f_contents[$_GET['quote']];
echo $line
}
更改锚点 link 以适应!