file - How to properly use "INCLUDE" in PHP script -
i have php script goes through list of movie data , parses data return movie title , rating several businesses. i'd have data parsing script single file included in several different files, if need edit movie data parser script, same every file it's included in.
this code of data parsing script works perfectly. filename, need replace variable, when included in file, can change filename variable name of correct movie list. originally, had each business own php data parsing script, it's not maintainable.
foreach (glob('mov/filename.*mov') $filename){ $thedata = file_get_contents($filename) or die("unable 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 question:
in data parsing script. how implement variable says 'filename', , when include script in file. how assign variable correct filename , output/run script?
you should implement code in include file function, , filename
should parameter function. other scripts can include file , call function appropriate filename.
similarly, $thedata
should return value of function, not global variable.
Comments
Post a Comment