[英]How to properly use “INCLUDE” in PHP script
I have a PHP script that goes through a list of movie data and parses that data to return the movie title and rating for several businesses. 我有一个PHP脚本,该脚本遍历电影数据列表并解析该数据以返回多家公司的电影标题和评级。 I'd like to have the data parsing script as a single file to be included in several different files, so if I need to edit the movie data parser script, it will be the same for every file it's included in.
我想将数据解析脚本作为一个文件包含在多个不同的文件中,因此,如果我需要编辑电影数据解析器脚本,则其中包含的每个文件都将是相同的。
This is the code of the data parsing script that works perfectly. 这是完美运行的数据解析脚本的代码。 for the filename, I need to replace that with a variable, so when included in another file, I can change that filename variable to the name of the correct movie list.
对于文件名,我需要将其替换为变量,因此当包含在另一个文件中时,我可以将该文件名变量更改为正确的电影列表的名称。 Originally, I had each business their own php data parsing script, but it's not very maintainable.
最初,我每个企业都有自己的php数据解析脚本,但是它不是非常可维护的。
foreach (glob('mov/FILENAME.*mov') as $filename){
$theData = file_get_contents($filename) or die("Unable to retrieve file data");
}
$string = $theData;
$titles = explode("\n", $string);
function getInfo($string){
$Ratings = ['G', 'PG', 'PG-13', 'R', 'NR', 'XXX'];
$split = preg_split("/\"(.+)\"/", $string, 0, PREG_SPLIT_DELIM_CAPTURE);
if(count($split) == 3){
preg_match("/(".implode("|", $Ratings).")\s/", $split[0], $matches);
$rating = $matches[0];
return ["title" => $split[1], "rating" => $rating];
}
return false;
}
FINALLY TO THE QUESTION: 最后的问题:
In the data parsing script. 在数据解析脚本中。 How would I implement a variable where it says 'FILENAME', and when I include this script in another file.
当我将此脚本包含在另一个文件中时,如何实现变量“ FILENAME”? How would I assign the variable to the correct filename and output/run the script?
如何将变量分配给正确的文件名并输出/运行脚本?
You should implement the code in the include file as a function, and FILENAME
should be a parameter to the function. 您应该将包含文件中的代码实现为函数,而
FILENAME
应该是该函数的参数。 Then all the other scripts can include the file and call the function with the appropriate filename. 然后,所有其他脚本可以包含该文件,并使用适当的文件名调用该函数。
Similarly, $theData
should be the return value of the function, not a global variable. 同样,
$theData
应该是函数的返回值,而不是全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.