(很抱歉,如果标题是相当无用)如何从preg_match_all中获得随机结果?
我有这样的功能,从在WordPress随机获得后第一个图像。这很好,但现在我需要它从所有匹配中选择一个随机图像,而不是第一个。 (我运行这个功能在query_posts循环选择类别)
// Get first image in post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
//no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
// Or, show first image.
return $first_img;
}
因此,任何想法,链接,提示&技巧如何选择比赛结果的随机结果?
工作正常!对于其他人,这是最终的代码。 '//获取帖子中的第一个图片 function catch_that_image(){ \t global $ post,$ posts; \t $ first_img =''; \t ob_start(); \t ob_end_clean(); \t $ output = preg_match_all('//i',$ post-> post_content,$ matches); \t $ first_img = $ matches [1] [0]; \t \t //没有发现图像显示缺省图像,而不是 \t如果(空($ first_img)){ \t \t $ first_img =“/图片/默认值。JPG“; \t} \t \t //或者,显示第一图像 \t回$匹配[1] [array_rand($比赛[1]);} ” –
PaulDavis
2010-01-25 14:53:50
好了,我不能让代码(UX错误?) 下面是最终的代码,格式不错。http://kodery.com/119 – PaulDavis 2010-01-25 15:07:24