在php中从html页面搜索字符串
我想这就是你想要的: 您根本不需要正则表达式 这是您需要的代码 试试这个在php中从html页面搜索字符串,php,search,preg-match,Php,Search,Preg Match,我想这就是你想要的: 您根本不需要正则表达式 这是您需要的代码 试试这个 <?php $data = file_get_contents("http://localhost/traveller/index.html"); $regex = '$_POST[/"search"/]'; if (isset($_POST["sub"])){ if (preg_match($regex, $data)) { echo $regex; } else { echo "n
<?php
$data = file_get_contents("http://localhost/traveller/index.html");
$regex = '$_POST[/"search"/]';
if (isset($_POST["sub"])){
if (preg_match($regex, $data)) {
echo $regex;
} else {
echo "not found";
}
}
?>
<form action="searching.php" method="POST">
Search: <br>
<input type="text" name="search">
<input type="submit" name="sub">
</form>
搜索:
我想你想要:
<?php
$data = file_get_contents("http://localhost/traveller/index.html");
if (isset($_POST["search"])&&!empty($_POST["search"])) {
$search_str = $_POST["search"];
if (strpos($data,$search_str)!== false) {
echo $search_str;
}
else {
echo "not found";
}
}
?>
<form action="searching.php" method="POST">
Search: <br>
<input type="text" name="search">
<input type="submit" name="sub">
</form>
您的正则表达式格式不正确:我想它应该是
$regex='/$\u POST\[“search”\]/'代码>在进行您所说的更改后,即使该页面上存在字符串,也只执行其他部分正则表达式是正确的。这没什么问题。你可以在这里的@adityaaundhekar检查正则表达式的真实性,因为你把整个程序写错了。我现在将用代码编辑我的答案。请检查一下。
$data = file_get_contents("http://localhost/traveller/index.html");
$regex = $_POST["search"];
if (isset($_POST["sub"])) {
if (preg_match('/' . preg_quote($regex) . '/i', $data)) {
echo $regex;
} else {
echo "not found";
}
}