sKunKbad Posted April 5, 2008 Share Posted April 5, 2008 I've got a preg match that works: if(preg_match("|$searchText|i",$contents)){ but if I add ^ and $ like this: if(preg_match("|^$searchText$|i",$contents)){ it stops working. Quote Link to comment https://forums.phpfreaks.com/topic/99748-variable-in-preg_match-how-to-apply-and/ Share on other sites More sharing options...
uniflare Posted April 5, 2008 Share Posted April 5, 2008 ^ 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? Quote Link to comment https://forums.phpfreaks.com/topic/99748-variable-in-preg_match-how-to-apply-and/#findComment-510187 Share on other sites More sharing options...
sKunKbad Posted April 5, 2008 Author Share Posted April 5, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/99748-variable-in-preg_match-how-to-apply-and/#findComment-510195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.