使用PHP脚本的HTML
我是php编程新手,最近遇到了一个问题。我有一个带有搜索框的html页面和一个php脚本,在本地主机上的特定文件中使用grep。这就是我想要的,当用户输入char字符串并单击enter,发送一篇文章来修改我的php var$contents_列表,并在找到字符串的地方grep all filename时使用PHP脚本的HTML,php,html,grep,Php,Html,Grep,我是php编程新手,最近遇到了一个问题。我有一个带有搜索框的html页面和一个php脚本,在本地主机上的特定文件中使用grep。这就是我想要的,当用户输入char字符串并单击enter,发送一篇文章来修改我的php var$contents_列表,并在找到字符串的地方grep all filename时 我的建议: 为了简单起见,将代码合并到单个index.php文件中 搜索和输出的独立逻辑,以实现职责的清晰分离 添加辅助文本,例如未找到任何内容或输入文本 index.php内容: &
我的建议:
- 为了简单起见,将代码合并到单个
文件中index.php
- 搜索和输出的独立逻辑,以实现职责的清晰分离
- 添加辅助文本,例如
或未找到任何内容
输入文本
index.php
内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<form action="index.php" method="post">
<input type="text" placeholder="search" name="search">
</form>
<?php
// Check if the form was submitted.
if (isset($_POST['search']) && (strlen($_POST['search'])) > 0) {
$search_line = $_POST['search'];
$path = "/my/directory/used/for/grep";
$dir_content = new RecursiveDirectoryIterator($path);
// Array to store results.
$results = [];
// Iterate through directories and files.
foreach (new RecursiveIteratorIterator($dir_content) as $filename => $file) {
$fd = fopen($file, 'r');
if ($fd) {
while (!feof($fd)) {
$file_line = fgets($fd);
if (strpos($file_line, $search_line) !== FALSE) {
$results[] = $filename;
}
}
fclose($fd);
}
}
// Output result.
echo "<pre>";
if ($results) {
foreach ($results as $index => $result) {
echo ($index + 1) . " :: $result\n";
}
}
else {
echo "Nothing found!";
}
echo "</pre>";
}
else {
// When nothing to search.
echo "<pre>Enter something to search.</pre>";
}
?>
</body>
</html>
如果您想在浏览器在后台向服务器发送请求时留在页面上,那么我的代码中有错误吗?非常感谢您已经解决了我的问题,您是否认为可以编写php搜索框,并且当我键入字符串时,脚本将grep到我服务器上的所有数据库(phpmyadmin)并返回数据库、列名称和行?是。这是可能的。首先,您的数据库可能是mysql
或mariadb
,而不是phpmysql
。其次,您需要将扫描文件
部分替换为查询数据库
。您可以在此处找到示例: