Page 1 of 1

Searching file names in a directory...

Posted: Mon Oct 30, 2006 7:04 pm
by Mr Tech
I want to create a script that searches file names in a certain directory... So for example, if I put all the file name in an array:

array('page1.html', 'page2.html', 'page3.html');

I could then use in_array to search within it...

That is fine if you search for the whole file name.... But what if someone searches for page or 1? How would I get the script to find it in the array?

Just to make myself clear, if someone typed in page for their search, it would extract all the pages from the array because they all have the word page within them.

Posted: Mon Oct 30, 2006 7:26 pm
by angelena
http://sg.php.net/manual/en/function.similar-text.php

but i felt isn't this will be much easier to achieve using call from db?So that you can easily do a "LIKE" query to achieve the search result.

Posted: Mon Oct 30, 2006 8:10 pm
by John Cartwright
This sparked my interest in searching dir's for patial matches, perhaps this could be of some use to you

Code: Select all

function searchDir($dir = '', $pattern = '') 
{	
   return glob((substr($dir, -1, 1) == '/' ? $dir : $dir.'/') . '*'. $pattern.'*');
}

Posted: Mon Oct 30, 2006 8:26 pm
by Mr Tech
I've found something that works but is it possible to display the matched results from the array?

So for example, if I used:

Code: Select all

preg_array("/page/i", $pages)
It would return the following:

page1.html
page2.html
page3.html


I have no idea how I would achieve this. Here's the function:

Code: Select all

<?php
function preg_array($pattern, $array)
{
  $match = 0;
   foreach ($array as $key => $value)
       {
         if (preg_match($pattern, $value))
             {
               $match = 1;
               break;
       }
   }
 return ($match == 1) ? true : false;
}

$pages = array('page1.html', 'page2.html', 'page3.html');

if (preg_array("/page1/i", $pages)) {
echo "Match found!";
}
else {
echo "Match not found!";
}
?>

Posted: Mon Oct 30, 2006 8:40 pm
by John Cartwright
Decided to write a proper snipplet for this, not properly tested though as I have to run for bit. I will follow up on this if you wish.

Code: Select all

<?php

class scanFiles
{
	protected $pattern, $dir, $extension;
	
	/**
	 * Constructor
	 * @param $pattern
	 * @param $dir
	 * @param $extension
	 *
	 * Checks possible invalid values and assigns default 
	 * values
	*/
	public function __construct($pattern = '', $dir = '', $extension = '') 
	{
		$this->directory = $this->getDir($dir);
		$this->pattern = $this->getPattern($pattern);
		$this->extension = $this->getExtension($extension);
	}
	
	/**
	 * search
	 *
	 * Perform search of directory
	 * @returns array
	*/
	public function search()
	{
		return glob($this->directory. '*'. $this->pattern.'*' . '.'. $this->extension);
	}	
	
	/**
	 * getPattern
	 *
	 * Checks if pattern is an array, and if so format
	 * it to support glob() formatting
	 * @param $pattern
	 * @returns string
	*/
	protected function getPattern($pattern)
	{
		if (is_array($this->pattern) && count($this->pattern)) 
		{
			$pattern = '{' . implode(',', $this->pattern) .'}';
		} 
		
		return $pattern;			
	}
	
	/**
	 * getDir
	 *
	 * If a directory was supplied we want to make sure it 
	 * exists. Also checks whether to add an additional 
	 * directory seperator
	 * @param $dir
	 * @returns string
	*/	
	protected function getDir($dir) 
	{
		if (!empty($dir) && !file_exists($dir)) {
			throw new Exception('Directory "'. $dir .' cannot be found');
		}
		
		return (substr($dir, -1, 1) == DIRECTORY_SEPARATOR ? $dir : $dir . DIRECTORY_SEPARATOR);
	}

	/**
	 * getExtension
	 *
	 * Check whether to supply a wildcard or extension
	 * name 
	 * @param $extension
	 * @returns string
	*/	
	protected function getExtension($extension)
	{
		return (empty($extension) ? '*' : $extension);
	}	
}


?>
1. This snipplet will allow you to pass an array for glob to search.
2. You can also set it to search for only a specific extension.
3. Checks whether you are searching a directory that exists
4. Searches in front and behind filename.

Posted: Mon Oct 30, 2006 9:47 pm
by Mr Tech
You're a legend! I've give it a wizz.

Posted: Mon Oct 30, 2006 10:11 pm
by Mr Tech
Error: Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in W:\www\test.php on line 5

I can't figure it out.

Posted: Mon Oct 30, 2006 10:28 pm
by feyd
I'll guess you're running PHP 4. Jcart's snippet is a PHP 5 one, whoever it's easily adjusted to PHP 4. Take a look at the PHP manual's migration sections.

Posted: Mon Oct 30, 2006 11:42 pm
by John Cartwright
I posted the final version in the Code Critique Forum. :arrow: viewtopic.php?p=324977#324977

I've also included a PHP4 version in there as well.