My code is:
Code: Select all
<?php
// LOGIC:
// constants:
define(HREF, "href");
define(META, "meta");
// variables:
$files = array();
// 'private' functions:
/*
* retrieve names of all files within $basedir (not incl. sub dirs).
* return as an array of strings
*/
function loadFileArray ( $basedir ) {
$filearray = array();
if ( is_dir( $basedir ) ) {
if ( $dhandle=opendir( $basedir ) ) {
while ( ( $file=readdir( $dhandle ) ) !== false) {
$tmp = array();
$tmp[HREF] = $file;
$filearray[] = $tmp;
}
closedir($dhandle);
}
}
return $filearray;
}
/*
* scan all filenames in $filearray testing whether the name
* matches the regular expression $filterexpr.
* if $match=true return an array of matching files,
* otherwise return an array of unmatching files.
*/
function filterFilesByName ( $filearray, $filterexpr, $match ) {
// init:
$matchfiles = array();
$unmatchfiles = array();
// parse:
for ( $i=0; $i < count( $filearray ); $i++) {
if ( preg_match( $filterexpr, $filearray[$i][HREF] ) ) {
$matchfiles[] = $filearray[$i];
} else {
$unmatchfiles[] = $filearray[$i];
}
}
// return:
if ( $match )
return $matchfiles;
else
return $unmatchfiles;
}
/*
* load all meta-tags' contents into associative array of
* integer indexed arrays:
*
* results = { "meta-name1" -> {0->"content1", 1->"content2"},
* "meta-name2" -> {0->"content1", 1->"content2"},
* "meta-name3" -> {0->"content1", 1->"content2"}
* }
*/
function loadMetaArray ( $href ) {
// init:
$meta = array();
// parse:
$tmp = get_meta_tags( $href );
while( list( $name, $content ) = each( $tmp ) ) {
$meta[$name] = array();
$meta[$name] = explode( ", ", $content );
}
// return results:
return $meta;
}
/*
* scan all filenames in $filearray testing whether their
* meta-content contains $filterexpr.
* if $match=true return an array of matching files,
* otherwise return an array of unmatching files.
*/
function filterFilesByContent ( $filearray, $filterexpr, $match ) {
// init:
$matchfiles = array();
$unmatchfiles = array();
// parse:
for ( $i=0; $i < count( $filearray ); $i++) {
$isMatch = false; // reset flag
while( list( $name, $content ) = each( $filearray[$i][META] ) ) {
for ( $i2=0; $i2 < count( $content ); $i2++) {
if ( preg_match( $filterexpr, $content[$i2] ) )
$isMatch = true; // flag file as matching filter
}
}
if ( $isMatch ) {
$matchfiles[] = $filearray[$i];
} else {
$unmatchfiles[] = $filearray[$i];
}
}
// return:
if ( $match )
return $matchfiles;
else
return $unmatchfiles;
}
?>
<html>
<head></head>
<body>
<?php
// PRESENTATION:
// obtain an array of all files in current dir:
$files = loadFileArray ( getcwd() );
// filter files by filename:
$files = filterFilesByName( $files, "/.*\.htm/", true);
$files = filterFilesByName( $files, "/\bcs-.*/", false);
$files = filterFilesByName( $files, "/\btx-.*/", false);
// load meta data as additional 'field' to file 'record':
for ($i=0; $i<count($files); $i++)
{
$files[$i][META] = loadMetaArray ( $files[$i][HREF] );
}
// filter files by meta-content:
$files = filterFilesByContent( $files, "/sport/", true );
// echo to screen:
for ( $i=0; $i < count( $files ); $i++) {
echo $files[$i][HREF]." contains:<br />";
while( list( $name, $content ) = each( $files[$i][META] ) ) {
echo $name.": ";
for ( $i2=0; $i2 < count( $content ); $i2++) {
echo $content[$i2]." ";
}
echo "<br />";
}
}
echo "<br />";
echo "<br />";
?>
</body>
</html>after calling filterFilesByContent( $files, "/sport/", true ); I lose the contents of $files[$i][META]. Am I making by value / by reference errors?
I'm kind of new to php so is this even the best way to structure the file related data?
please help.