Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中从html页面搜索字符串_Php_Search_Preg Match - Fatal编程技术网

在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";
    }
}