使用PHP打开HTML和JS文件并搜索特定关键字

时间:2018-04-27 16:25:18

标签: php file search keyword

我正在编写一个小脚本,区分响应广告横幅和非响应广告横幅。 我需要扫描这些横幅的HTML和JS文件以获取以下关键字: Android,Iphone,如果发现了某些内容,则返回true或false。

$fileinfos = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory)
);
foreach ($fileinfos as $pathname => $fileinfo) {
if (!$fileinfo->isFile()) continue;
//var_dump($pathname);
$files[] = $pathname;
}

$array_count = count($files); //Counts all elements of the array - will 
be used for the loop

$info = array();
$match_files = array();
$index_match = 0;

for ($i = 0; $i < $array_count; $i++) {
    //echo "<br>" . $files[$i];

$info[$i] = pathinfo($files[$i]);

if ($info[$i]['extension'] == 'html' || $info[$i]['extension'] == 'js') 
{
    //echo $info[$i]['dirname']. "/" . $info[$i]['basename']. "<br>";
    $match_files[$index_match++] = $info[$i]['dirname'] . "/" . 
$info[$i]['basename'];

}


}

此代码返回Array中特定文件夹的html和js文件的路径。 现在我想扫描这些文件以获取上述关键字。 我希望你能在这里帮助我,这真让我头疼。

和平与问候 人

1 个答案:

答案 0 :(得分:1)

只需将整个文件存储在字符串中即可。使用strpos传入文件和您正在查找的关键字,如果找不到它将返回false,或者在字符串中找到它的索引。您可以使用该索引对关键字进行进一步分析,以确保它不在评论中。

http://php.net/manual/en/function.strrpos.php