从TXT文件中提取随机行作为字符串 [英] Pull random line from TXT file as string

查看:89
本文介绍了从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文件中提取随机行作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆