php正则表达式或preg_匹配颜色框(jQuery)
我有一个广告网站,上面有图片,名称如下:php正则表达式或preg_匹配颜色框(jQuery),php,regex,preg-match,colorbox,Php,Regex,Preg Match,Colorbox,我有一个广告网站,上面有图片,名称如下: images/139/16_2_139.jpg 其中:- images/139/是用户文件夹 16是ad的id 2是此广告可用的图像数 139是用户id 例子 images/1390/3800_12_1390.jpg images/27/728_7_27.jpg images/8563/13281_2_8563.jpg 现在我需要在幻灯片(彩色框)中显示与此广告相关的所有图像 我从sql中提取广告id和用户id 我需要php在目录中读取每个关于广
images/139/16_2_139.jpg
其中:-
是用户文件夹images/139/
是ad的id16
是此广告可用的图像数2
是用户id139
images/1390/3800_12_1390.jpg
images/27/728_7_27.jpg
images/8563/13281_2_8563.jpg
现在我需要在幻灯片(彩色框)中显示与此广告相关的所有图像
我从sql中提取广告id和用户id
我需要php在目录中读取每个关于广告id和用户id的图像,并在colorbox幻灯片(jQuery插件)中显示它
广告id从1到15000,用户id从1到9000,也可以更多
我不知道更好的regex或preg_match是否能解决我的问题
谢谢你的帮助试试这个
$str='images/1390/3800_12_1390.jpg
images/27/728_7_27.jpg
images/8563/13281_2_8563.jpg';
preg_match_all('#images/([0-9]+)/([0-9]+)_([0-9]+)_([0-9]+)#is',$str,$res);
$r=array();unset($res[0]);
foreach($res as $k=>$v){
foreach($v as $k2=>$v2){
$r[$k2][$k]=$v2;
}
}
print_r($r);
我为自己找到了一个解决办法谁工作 我分享以下内容供参考:
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';
如果有人对此有更好的解决方案,请发布
谢谢大家
;-) 大家好,如果我有例子:16_1_139.jpg、16_2_139.jpg、16_3_139.jpg、16_6_139.jpg、27/728_7_139.jpg、13281_12_139.jpg等等,我只需要显示文件夹中16号广告和139号用户的这些图像:meens all images 16_XX_139.jpg在本例中为4个图像。谢谢
matches images found: 3
preg_match brauch : 0.00037693977355957 Sekonnen