Search code examples
phpregexpreg-matchcolorbox

php regex or preg_match colorbox (jQuery)


i have an ads site where images are located and named as the follow:

images/139/16_2_139.jpg

where: -

  • images/139/ is the users folder
  • 16 is the id of ad
  • 2 is the number of images available for this ad
  • 139 is the user id

examples

images/1390/3800_12_1390.jpg
images/27/728_7_27.jpg
images/8563/13281_2_8563.jpg

Now I need to display every images concerning this ad in a slideshow (colorbox)

i extract the ad id and the user id from sql

i need that php read in the directory every images concerning the ad id and the user id and display it in colorbox slideshow (jQuery addon) the ad id goes from 1 to 15000 and the user id goes from 1 to 9000 and can also be more

i dont know if better regex or preg_match can solve my problem

thanks for your help


Solution

  • i found myself a solution who work i share for reference:

    function microtime_float()
    {
       list($usec, $sec) = explode(' ', microtime());
    
       return ((float)$usec + (float)$sec);
    }
    ini_set ("display_errors", "1");
    error_reporting(E_ALL);
    $start = microtime_float();
    $b = '<br />';  $ad = 16;   $cust = 139;
    $directory = $_SERVER['DOCUMENT_ROOT'].'/ginew/advertiser/images/'. $cust .'/';
    // echo $directory;
    if( is_dir( $directory ) ) {
    $handler = opendir( $directory );
        $images = glob("$directory{*.jpg,*.JPG,*jpeg, *JPEG}", GLOB_BRACE);
        $cnt = count($images);
    echo 'total images in directory: '. $cnt.$b;
    
    $i=0;   $img1 = '';
    while ($filename = readdir($handler)) {
        if($filename != '.' && $filename != '..') {
            $base = basename($filename, ".jpg"); // echo $base.$b;
            $ext = substr(strrchr($filename, '.'), 0); // echo $ext.$b;
            $imgsize = @filesize($directory.$filename); $imgsize =     ($imgsize/1000000) .' mb';
    if ( preg_match( '#'.$ad.'_([0-9]+)_'.$cust.'#', $base, $matches ) ) { 
    $i++;
     $img = $i .' matches '. $matches[0] . $ext; 
    $img1.= $i .' matches '. $matches[1] .'<br>'; 
    
     echo $img.$b;
            } // END is_dir
              echo $i .' --> '. $filename . $ext .' => '. $imgsize .'<br />';
        } // END preg_match
    } // END if filename
    $end = microtime_float();
    } // END while
    echo '/ -------------------------- /<br>';
    echo $img1.$b;
    echo 'matches images found: '. $i . $b;
    echo 'preg_match brauch : '.($end-$start).' Sekonnen';
    

    output:
    total images in directory: 6
    1 matches 16_2_139.jpg
    2 matches 16_7_139.jpg
    3 matches 16_1_139.jpg
    / -------------------------- /
    1 matches 2
    2 matches 7
    3 matches 1

    matches images found: 3
    preg_match brauch : 0.00037693977355957 Sekonnen
    

    if anybody has a better solution for this please post it thanks to everyone ;-)