<?php

創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè),為客戶提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)開(kāi)發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開(kāi)發(fā),成都品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。
/**
* 文件: search.php
* 功能: 搜索指定目錄下的HTML文件
*/
/* 基本函數(shù) */
//獲取目錄下文件函數(shù)
function getFile($dir)
{
$dp = opendir($dir);
$fileArr = array();
while (!false == $curFile = readdir($dp)) {
if ($curFile!="." && $curFile!=".." && $curFile!="") {
if (is_dir($curFile)) {
$fileArr = getFile($dir."/".$curFile);
} else {
$fileArr[] = $dir."/".$curFile;
}
}
}
return $fileArr;
}
//獲取文件內(nèi)容
function getFileContent($file)
{
if (!$fp = fopen($file, "r")) {
die("Cannot open file $file");
}
while ($text = fread($fp, 4096)) {
$fileContent .= $text;
}
return $fileContent;
}
//搜索指定文件
function searchText($file, $keyword)
{
$text = getFileContent($file);
if (preg_match("/$keyword/i", $text)) {
return true;
}
return false;
}
//搜索出文章的標(biāo)題
function getFileTitle($file, $default="None subject")
{
$fileContent = getFileContent($file);
$sResult = preg_match("/<title>.*</title>/i", $fileContent, $matchResult);
$title = preg_replace(array("/(<title>)/i","/(</title>)/i"), "", $matchResult[0]);
if (empty($title)) {
return $default;
} else {
return $title;
}
}
//獲取文件描述信息
function getFileDescribe($file,$length=200, $default="None describe")
{
$metas = get_meta_tags($file);
if ($meta[description] != "") {
return $metas[description];
}
$fileContent = getFileContent($file);
preg_match("/(<body.*</body>)/is", $fileContent, $matchResult);
$pattern = array("/(<[^x80-xff] >)/i","/(<input.*>) /i", "/(<a.*>) /i", "/(<img.*>) /i", "/([<script.*>]) .*([</script>]) /i","/&/i","/"/i","/'/i", "/s/");
$description = preg_replace($pattern, "", $matchResult[0]);
$description = mb_substr($description, 0, $length)." ...";
return $description;
}
//加亮搜索結(jié)果中的關(guān)鍵字
function highLightKeyword($text, $keyword, $color="#C60A00")
{
$newword = "<font color=$color>$keyword</font>";
$text = str_replace($keyword, $newword, $text);
return $text;
}
//獲取文件大小(KB)
function getFileSize($file)
{
$filesize = intval(filesize($file)/1024)."K";
return $filesize;
}
//獲取文件最后修改的時(shí)間
function getFileTime($file)
{
$filetime = date("Y-m-d", filemtime($file));
return $filetime;
}
//搜索目錄下所有文件
function searchFile($dir, $keyword)
{
$sFile = getFile($dir);
if (count($sFile) <= 0) {
return false;
}
$sResult = array();
foreach ($sFile as $file) {
if (searchText($file, $keyword)) {
$sResult[] = $file;
}
}
if (count($sResult) <= 0) {
return false;
} else {
return $sResult;
}
}
/* 測(cè)試代碼 */
//指定要搜索的目錄
$dir = "./php_Linux";
//要搜索的關(guān)鍵字
$keyword = "sendmail";
$fileArr = searchFile($dir, $keyword);
$searchSum = count($fileArr);
echo "搜索關(guān)鍵字: <b>$keyword</b> 搜索目錄: <b>$dir</b> 搜索結(jié)果: <b>$searchSum</b><br><hr size=1><br>";
if ($searchSum <= 0) {
echo "沒(méi)有搜索到任何結(jié)果";
} else {
for
本文題目:php查找文件內(nèi)容關(guān)鍵字實(shí)例代碼
網(wǎng)頁(yè)URL:http://chinadenli.net/article30/ipsoso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)站收錄、企業(yè)建站、網(wǎng)站設(shè)計(jì)公司、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)