Pull random line from TXT file as string(从 TXT 文件中提取随机行作为字符串)
问题描述
我目前正在使用下面的代码尝试从 random.txt
中读取随机行并将其作为 $data
传递,但它返回空.每个字符串都有自己的行,我在这里遗漏了什么吗?这不应该工作吗?如果不是,我如何从我的文本文件中获取随机行并将其用作我的 $data
字符串?
I'm currently using the code below to try and read a random line from random.txt
and pass it as $data
, however it comes back empty. Each string has its own line, am I missing something here? Shouldn't this work? If not how can I get a random line from my text file and use it as my $data
string?
$f_contents = file("random.txt");
$line = $f_contents[array_rand($f_contents)];
$data = $line;
已解决 - 错误的 CHMOD以为我已经仔细检查过了,很抱歉提出问题.
Solved - Bad CHMOD Thought I had double checked it, sorry to post a question.
推荐答案
确保您的文件设置了读取权限,应该 CHMOD 为 644 或 744.
Make sure your file has read permissions set, should be CHMOD'd to 644 or 744.
这篇关于从 TXT 文件中提取随机行作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 TXT 文件中提取随机行作为字符串


基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01