Jump to content

Recommended Posts

^ might not work unless your running apache. use \A instead. Also use literal strings in patterns (it helps):

 

eg:

if(preg_match('|\A'.$searchText.'$|i',$contents)){

 

Also this expression is useless as it is the same as using:

if($searchText == $contents){

----------

 

what are you trying to do with this expression?

I'm just trying to make a non-database search for files/pages on my website. I'm not really sure if this is the way to do it, but this is the direction I'm currently headed. Feel free to make suggestions.

 

<?php
//a search term for testing purposes - later to be imported via POST and validated/filtered

$searchText = "avneri";


//the following code makes an array of file names to search from the menuList.inc.php
require "menuList.inc.php";
preg_match_all("|href=\".*\"|",$menu, $matches);
$pageList = array("index.php");
for ($i=1;$i<count($matches[0]);$i++){
	$match = preg_split("|\"|",$matches[0]["$i"]);
	$pageList[] = $match[1];
}

//This is the list of files that will be searched
echo "<pre>";
print_r($pageList);
echo "</pre>";

//search each file from the $pageList array, and determine if the $searchText is present
foreach($pageList as $page){
	$handle = fopen("$page", "r");
	$contents = fread($handle, filesize($page));
	if(preg_match("|\\b$searchText\\b|i",$contents)){
		$found[] = $page;
	}
}

//this is the array of files in which the search term was found.
echo "<pre>";
print_r($found);
echo "</pre>";
?>

 

menuList.inc.php:

<?php
$menu=<<<MENU
<ul>
<li class="sect first">Site Menu</li>
<li class="item"><a title="Brian's Web Design - Home Page" href="http://www.brianswebdesign.com/">Home</a></li>
<li class="item"><a title="Brian's recent website portfolio" href="portfolio.php">Portfolio</a></li>
<li class="item"><a title="Information about Brian" href="about.php">About</a></li>
<li class="item"><a title="Frequently Asked Questions" href="faq.php">FAQs</a></li>
<li class="item"><a title="Contact Brian" href="contact.php">Contact</a></li>
<li class="item"><a title="Get an Estimate" href="estimates.php">Get an Estimate</a></li>
<li class="item"><a title="Accessibility Statement" href="accessibility.php">Accessibility</a></li>
</ul>
MENU;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.