CodeIgniter Forums

Full Version: Search files question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I search for file with any of the key search word in string.

Currently example if my key search is number 3 it will find files that start with the number 3.jpg

How ever for example I would like to be able to find files that have 123.jpg also. I need to be able to search for letters and numbers.


PHP Code:
if (null !==($this->input->get('directory'))) {
$directory $this->security->xss_clean(DIR_IMAGE .'catalog/'$this->input->get('directory'TRUE));
} else {
    $directory $this->security->xss_clean(DIR_IMAGE .'catalog');
}

if (
$this->input->post('search')) {
    $filter_name $this->input->post('search'TRUE);
} else {
    $filter_name null;
}

 // Get directories
$directories glob($directory '/' $filter_name '*'GLOB_ONLYDIR);

if (!
$directories) {
    $directories = array();
}

// Get files
$files glob($directory '/' $filter_name '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}'GLOB_BRACE);

natsort($files);

if (!
$files) {
      $files = array();
}

// Other Code 
From stackoverflow.

Consider using preg_replace_callback.

Use this regex: (images/([0-9]+)[^"]+")

Then, as the callback argument, use an anonymous function. Result:

You should be able to modify this to work for you.

PHP Code:
$output preg_replace_callback(
 
   "(images/([0-9]+)[^\"]+\")",
 
   function($m) {
 
       // $m[1] is the number.
 
       $t getTitleFromDatabase($m[1]); // do whatever you have to do to get the title
 
       return $m[0]." title=\"".$t."\"";
 
   },
 
   $input
); 

Give it a try.
(08-29-2017, 03:36 AM)InsiteFX Wrote: [ -> ]From stackoverflow.

Consider using preg_replace_callback.

Use this regex: (images/([0-9]+)[^"]+")

Then, as the callback argument, use an anonymous function. Result:

You should be able to modify this to work for you.

PHP Code:
$output preg_replace_callback(
 
   "(images/([0-9]+)[^\"]+\")",
 
   function($m) {
 
       // $m[1] is the number.
 
       $t getTitleFromDatabase($m[1]); // do whatever you have to do to get the title
 
       return $m[0]." title=\"".$t."\"";
 
   },
 
   $input
); 

Give it a try.

OK I will I have also tried


PHP Code:
$files glob($directory '/*{' $filter_name '}*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}'GLOB_BRACE); 


Seems to work what you think of that above? Because I need to search letters and numbers.