if(preg_match_all("/\[photo([^\[\]]*)\]/", $text, $preg, PREG_PATTERN_ORDER)){
foreach($preg[0] as $key => $value){
$i = $preg[1][$key];
//$photos - массив с картинками
if( $photos[$i-1]['file'] ){
$block = "<div><img src="'.$photos[$i-1]['file'],.'"></div>';
}else{
$block = ' ';
}
$text = str_replace($value, $block, $text);
}
}