<?php
function parse_dir($path, $name)
{
$data = array();
for ($i=0; $i < 5; $i++) {
$res = glob("{$path}{$name}");
if (count($res)) {
$data[] = $res;
break;
}
$path .= '*/';
//echo "\t{$path}<br>"; // Debug output
}
return $data[0];
}
function file_parser($dir, $needed, $tags, $class)
{
$data = array();
// търсим всичи файлове, които са с разширение .html
$directories = parse_dir($dir, '*.html');
if ( sizeof($directories) > 0 )
{
foreach ( $directories as $dir )
{
if ( is_file($dir) )
{
$readFile = file_get_contents($dir);
preg_match_all('#<(' . implode('|', $tags) . ')\s*(class="(' . implode('|', $class) . ')")?\s*>\n?.*(' . $needed . ').*\n?<\/(' . implode('|', $tags) . ')>#i', $readFile, $matches);
if ( sizeof($matches[4]) ) {
$data[] = array('link' => $dir, 'target' => $matches[4][0]);
}
}
}
return $data;
}
}
$dir = "D:/wamp/www/WT/testDir/";
$needed = "i need";
$tags = array('div', 'p');
$class = array('simple-post', 'simple-article');
echo "<pre>";
print_r(file_parser($dir, $needed, $tags, $class));