简体   繁体   English

如何将PHP文件作为文本文件读取到数组?

[英]How to read a PHP file as text file to an array?

Here is my situation.Supposing I have a PHP file named as: 这是我的情况。假设我有一个名为的PHP文件:

myfunctions.php

Is it possible to read the contents of that PHP file (myfunctions.php) in another script using available PHP functions? 是否可以使用可用的PHP函数在另一个脚本中读取该PHP文件(myfunctions.php)的内容?

I'm using the following functions without success in reading this PHP file: 我正在使用以下功能,但未成功读取此PHP文件:

file_get_contents()
file()

It simply returned blank. 它只是返回空白。 One of the possible successful method was to read this PHP file as text file but I do not know how to do this..If someone has some other methods, please share. 可能的成功方法之一是将该PHP文件读取为文本文件,但我不知道该怎么做。如果有人有其他方法,请分享。 Ideally I want the output to be an array so the data I would like to obtain will be easily manipulated for use. 理想情况下,我希望输出是一个数组,这样我想要获取的数据将易于使用。

Thanks. 谢谢。

You need fread . 你需要恐惧 To put it into an array, you might look at explode 要将其放入数组中,您可以查看explode

If I were you, I will change the extension on the "myfunctions.php" to "myfunctions.txt" and use the theses functions 如果您是我,我将把“ myfunctions.php”上的扩展名更改为“ myfunctions.txt”,并使用这些函数

file_get_contents()
file()

But I don't know if you are allow to change the name of the document. 但是我不知道您是否允许更改文档名称。

file() - Reads entire file into an array
fgets() - Gets line from file pointer
fread() - Binary-safe file read
readfile() - Outputs a file
file_put_contents() - Write a string to a file
stream_get_contents() - Reads remainder of a stream into a string
stream_context_create() - Creates a stream context

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

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