(很抱歉,如果標題是相當無用)如何從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