Searching file names in a directory...
Moderator: General Moderators
Searching file names in a directory...
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.
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.
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.'*');
}I've found something that works but is it possible to display the matched results from the array?
So for example, if I used:
It would return the following:
page1.html
page2.html
page3.html
I have no idea how I would achieve this. Here's the function:
So for example, if I used:
Code: Select all
preg_array("/page/i", $pages)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!";
}
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.
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.
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);
}
}
?>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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I posted the final version in the Code Critique Forum.
viewtopic.php?p=324977#324977
I've also included a PHP4 version in there as well.
I've also included a PHP4 version in there as well.