繁体   English   中英

选择数组元素或删除前两个

[英]Choose array element or remove the first two

我正在改编生成缩略图的wordpress插件。 多亏了这个网站,我的美感才得以提高,但我陷入了一个问题。 该插件从帖子中的第一张图片生成缩略图。 我需要从第三张图像生成的图像。

帖子内容

[URL=http://example.com/viewer.php?file=3030128910131424.jpg]      [IMG]http://example.com/images/30301289149131424_thumb.jpg[/IMG][/URL] [URL=http://example.com/viewer.php?file=6331951274718152.jpg][IMG]http://example.com/images/7633127470118152_thumb.jpg[/IMG][/URL]

原始代码

// Initialize variable used to store list of matched images as per provided regular expression
$matches = array(); 

// Get all images from post's body
preg_match_all('/\[img\]([^\[\]>]*)/i', $post[0]->post_content, $matches); 

if (count($matches)) {
    foreach ($matches[0] as $key => $image) {
        /**
         * If the image is from wordpress's own media gallery, then it appends the thumbmail id to a css class.
         * Look for this id in the IMG tag.
         */
        preg_match('/wp-image-([\d]*)/i', $image, $thumb_id);
        $thumb_id = $thumb_id[1];

        // If thumb id is not found, try to look for the image in DB. Thanks to "Erwin Vrolijk" for providing this code.
        if (!$thumb_id) {
            $image = substr($image, strpos($image, '"')+1);
            $result = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE guid = '".$image."'");
            $thumb_id = $result[0]->ID;
        }

        // Ok. Still no id found. Some other way used to insert the image in post. Now we must fetch the image from URL and do the needful.
        if (!$thumb_id) {
            $thumb_id = apt_generate_post_thumb($matches, $key, $post[0]->post_content, $post_id);
        }

        // If we succeed in generating thumg, let's update post meta
        if ($thumb_id) {
            update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
            break;
        }
    }
}
}// end apt_publish_post()

 /**
  * Function to fetch the image from URL and generate the required thumbnails
 */

 function apt_generate_post_thumb($matches, $key, $post_content, $post_id)
 { 
 // Make sure to assign correct title to the image. Extract it from img tag
$imageTitle = '';
preg_match_all('/<\s*img [^\>]*title\s*=\s*[\""\']?([^\""\'>]*)/i', $post_content, $matchesTitle);

if (count($matchesTitle) && isset($matchesTitle[1])) {
    $imageTitle = $matchesTitle[1][$key];
}

// Get the URL now for further processing

$imageUrl = $matches[1][$key];
$imageUrl = str_replace("_thumb", "", $imageUrl);

我已经尝试了两件事

// Get the URL now for further processing

$imageUrl = $matches[3][$key];
$imageUrl = str_replace("_thumb", "", $imageUrl);

不生成缩略图。

现在,我试图在获取URL之前删除前两个数组变量。 我尝试了array_splice和array_slice,但是没有结果。 有什么想法要从第三个图像而不是第一个图像创建缩略图吗? 谢谢!

我建议explode($matches)然后在“ second”元素上运行您的procedure ,该元素在0 1 2计数中为3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM