繁体   English   中英

PHP的? 从.txt文件获取随机行,使其静态以在页面上创建url

[英]php? Get random line from .txt file, make it static to create url's on a page

我对编码一无所知,这是我设法运行的第一件事。

来了

我找到了这段代码

<?php
$lines = file('my_file.txt');
echo $lines[array_rand($lines)];
?>

它从my_file.txt中获取一条随机行并将其显示。

我想创建一个简单的.php页面,该页面会从my_file.txt中随机取一行(或者一个接一个?),并将其传递到我的超链接末尾,例如http://www.google.com/(random_line_from_my_file.txt)

例如: my_file.txt包含3行:

a
b
c

我希望我的.php页面显示http://www.google.com/ahttp://www.google.com/bhttp://www.google.com/c

有可能使这项工作吗?

还有一件事,是否有可能使选定的随机行在整个页面上保持静态,直到我们刷新页面?

例如:如果我想在我的简单.php页面上拥有3个超链接,那么它将始终显示:

http://www.google.com/a
http://www.bing.com/a
http://www.yahoo.com/a

而不是像

http://www.google.com/a
http://www.bing.com/b
http://www.yahoo.com/b

很容易:

<?php
    $lines = file ('file.txt', 
        FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $line = $lines [array_rand ($lines, 1)]; 
    foreach (array ("google", "bing", "yahoo") as $d) {
        $u = "www.$d.com/" . $line;
        echo "<a href=\"http://$u\">$u</a><br/>\n";
    }
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM