我在$ pages数组变量的给定页面中有下面提到的关键字搜索代码,它按我的意思工作,但由于$ pages数组中有多个页面,因此需要花费很多时间。如何提高这个速度?请告诉我。
<?php
$match="";
if(isset($_POST['submit'])){
if(isset($_POST['txt_search'])){
$match=trim($_POST['txt_search']);
}
if($match==""){
print "Please enter search text";exit;
}
}
<html>
<body>
<form action="" method="post">
<input type="text" name="txt_search" placeholder="Search term"/>
<input type="submit" value="Search" name="submit" onClick=""/>
</form>
<p id="text">
<?php if($match!=""){
mastersearch($match);
}?>
</p>
</body>
</html>
<?php
function mastersearch($match) {
$pages=array("index.php","about.php","contact.php","books.php","careers.php","shopping.php","travels.php","lifestyle.php","movies.php","museum.php");
foreach ($pages as $value) {
$text=$value;
$path="http://sitename.com/user/";
$value = $path.$value;
$json = file_get_contents($value);
$lastPos=0;
$last=strripos($json, $match);
while (($lastPos = stripos($json, $match, $lastPos))!== false) {
$positions[] = $lastPos;
preg_match("/<title>(.*)<\/title>/i", $json, $title);
print $title[1];
print "<br><a href='$value'>$text</a><br/>";
$lastPos = $lastPos + strlen($match);
}
}
}
?>
答案 0 :(得分:0)
如果你time your script,我认为file_get_contents()
电话是昂贵的。这取决于您与远程站点的连接,因此为了加快速度,您可以改善连接或引入某种缓存