程序员实用工具PHP代码搜索结果可折叠展开支持多词指定后缀

<?php
// 检查是否是POST请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$folder = $_POST['folder'];
$extensions = explode(',', $_POST['extensions']);
$extensions = array_map('trim', $extensions);
$searchTerms = explode(',', $_POST['searchTerms']);
$searchTerms = array_map('trim', $searchTerms);
$results = [];
// 递归搜索文件
function searchInFiles($dir, $extensions, $searchTerms) {
$results = [];
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir)
);
foreach ($files as $file) {
if ($file->isFile()) {
$extension = strtolower($file->getExtension());
if (in_array($extension, $extensions)) {
$filepath = $file->getRealPath();
$content = file_get_contents($filepath);
$lines = explode("\n", $content);
$matches = [];
foreach ($lines as $lineNum => $line) {
foreach ($searchTerms as $term) {
if (stripos($line, $term) !== false) {
$matches[] = [
'line' => $lineNum + 1,
'content' => htmlspecialchars($line)
];
break;
}
}
}
if (!empty($matches)) {
$results[] = [
'filename' => $filepath,
'matches' => $matches
];
}
}
}
}
return $results;
}
$results = searchInFiles($folder, $extensions, $searchTerms);
header('Content-Type: application/json');
echo json_encode($results);
exit;
}
?><!DOCTYPE html>
<html>
<head>
<title>文本内容查找工具</title>
<style>
body{font-family:Arial,sans-serif;max-width:800px;margin:20px auto;padding:0 20px;}
.form-group{margin-bottom:15px;}
label{display:block;margin-bottom:5px;}
select,input{width:100%;padding:8px;border:1px solid #ddd;border-radius:4px;}
button{background:#4CAF50;color:white;padding:10px 20px;border:none;border-radius:4px;cursor:pointer;}
.result-block{margin:15px 0;border:1px solid #ddd;border-radius:4px;}
.result-header{padding:10px;background:#f5f5f5;display:flex;justify-content:space-between;align-items:center;}
.result-content{padding:15px;display:none;}
.toggle-btn{background:#666;color:white;padding:5px 10px;border:none;border-radius:3px;cursor:pointer;}
.match-line{margin:5px 0;}
.match-content{color:red;}
</style>
</head>
<body>
<h1>文本内容查找工具</h1>
<form id="searchForm">
<div class="form-group">
<label>选择文件夹:</label>
<select name="folder" required>
<option value="">请选择文件夹</option>
<?php
$dirs = array_filter(glob('*'), 'is_dir');
foreach($dirs as $dir) {
echo "<option value='" . htmlspecialchars($dir) . "'>" . htmlspecialchars($dir) . "</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>文件后缀 (用逗号分隔,如: php,html,txt):</label>
<input type="text" name="extensions" required placeholder="输入文件后缀,用逗号分隔" value="php,asp,html,js,css,txt">
</div>
<div class="form-group">
<label>搜索内容 (用逗号分隔多个关键词):</label>
<input type="text" name="searchTerms" required placeholder="输入要搜索的内容,用逗号分隔" value=".php,.asp">
</div>
<button type="submit">开始搜索</button>
</form>
<div id="results"></div>
<script>
document.getElementById('searchForm').addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
fetch('?act=soso', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = '';
data.forEach(result => {
const resultBlock = document.createElement('div');
resultBlock.className = 'result-block';
const header = document.createElement('div');
header.className = 'result-header';
header.innerHTML = `
<span>${result.filename}</span>
<button class="toggle-btn">展开</button>
`;
const content = document.createElement('div');
content.className = 'result-content';
result.matches.forEach(match => {
const matchDiv = document.createElement('div');
matchDiv.className = 'match-line';
matchDiv.innerHTML = `第 ${match.line} 行: <span class="match-content">${match.content}</span>`;
content.appendChild(matchDiv);
});
resultBlock.appendChild(header);
resultBlock.appendChild(content);
resultsDiv.appendChild(resultBlock);
header.querySelector('.toggle-btn').addEventListener('click', function() {
const contentDiv = this.parentElement.nextElementSibling;
const isHidden = contentDiv.style.display === 'none' || contentDiv.style.display === '';
contentDiv.style.display = isHidden ? 'block' : 'none';
this.textContent = isHidden ? '收起' : '展开';
});
});
})
.catch(error => {
console.error('Error:', error);
alert('搜索过程中发生错误');
});
});
</script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YUJIANYUE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值