Hi all, Im trying to make a useful search bar for someone who wants to find a php/html term inside a textfile. (only aobut 50k max) At the moment im using arrays, but some searches I cannot search for. In my textfile I have things like:- #------html Explaination #------html/forms Explaination #------html/forms/button Explaination 1) I seem to be able to search for forms and button but not html. and 2) also it shows all the array elements below which I want to hide somehow. Heres the code:- <?php // // Class for finding a key in a multi-dimensional array // This is specific to my needs, not general // class key_search { var $found = array(); // Constructor method function key_search($search, $array = '', $case = 0) { // Set up the parameters $search = (!is_array($search)) ? array($search) : $search; $array = (!is_array($array)) ? array($array) : $array; $ereg = ($case) ? 'ereg' : 'eregi'; // // Search through the array keys for a match // // Loop through keys foreach (array_keys($array) as $key) { // Loop through search targets foreach ($search as $target) { // Return false if an empty string if ($target == '') { return false; } // If target expression matches the key and the resolved key in array exists if ($ereg($target, $key) && $array[$key][0]) { // Put the resolved key in the list $this->found[] = $array[$key][0]; } } } // Loop through values foreach ($array as $value) { // If the value is an array, search in it if (is_array($value)) { $this->key_search($search, $value, $case); } } } } // // Class for getting an array of tags from a file // class file_tags { var $list = array(); var $blocks = array(); // Constructor method function file_tags($file, $marker = '', $separator = '/') { // Get the raw contents of the file $contents = file_get_contents($file); // Explode the file into pieces using this special marker $this->blocks = explode($marker, $contents); // Start breaking the blocks down (ignore the first block, just extra) for ($i = 1; $i < count($this->blocks); $i++) { // Explode the tags into an array $tags = explode($separator, trim(substr($this->blocks[$i], 0, strpos($this->blocks[$i], "\n")))); // Create a string that looks like an array $string = ''; foreach ($tags as $tag) { $string .= "['$tag']"; } $string .= "[] = $i"; // Evaluate the string as a keyset and attach to $list eval("\$this->list$string;"); } } } // Search keywords $keywords = stripslashes(trim($_POST['search'])); // Search form echo '<form method="post" > Search for an Article: <input type="text" name="search" value="' . $keywords . '"> <input type="submit" name="submit" value="Submit"> </form>'; // Create a new tag list $file = new file_tags('PHPlibrary1.txt', '#------'); // Search for keywords in the list $search = new key_search($keywords, $file->list); ?> <pre> <?php // // Display // // Display found matches print_r($search->found); // Loop through found matches foreach ($search->found as $found) { // Extract the tag for this match $tag = trim(substr($file->blocks[$found], 0, strpos($file->blocks[$found], "\n"))); // Display the tag and its blocked content echo "\nTag: $tag\n" . strstr($file->blocks[$found], "\n"); '</a><div style="margin-top:5px;margin-bottom:2em;border:1px black solid;">'; } // Display the list print_r($file->list); ?> </pre> Code (markup):
Take the text file in a variable. and using preg_match search the keyword if match then display the content.
Hey all, it seems to search all the tag words now, and I forgot to ' // ' two lines near the bottom of my code to remove the array stuff. So, to search the whole text in the text file would I still use preg_match? I have not used it in code before. Is there a way to show 5 search matches at a time?