获取echo的最后一部分并存储在变量PHP中

时间:2012-05-03 14:23:32

标签: php function

我有一个函数可以回显Wordpress内容中图像的URL。

我现在的功能工作没有问题

// Get Image Attachments
function sa_get_image($postid=0, $size='thumbnail') { //it can be thumbnail or full
    if ($postid<1)
    $postid = get_the_ID();
    $thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
    if ($thumb != null or $thumb != '') {
        echo $thumb;
    }
    elseif ($images = get_children(array( //If you upload an image function gets first image
        'post_parent' => $postid,
        'post_type' => 'attachment',
        'numberposts' => '5',
        'post_mime_type' => 'image', )))
        foreach($images as $image) {
            $thumbnail=wp_get_attachment_image_src($image->ID, $size);
            ?>
    <?php echo $thumbnail[0]; ?>
    <?php }
        else { //If you don't upload or declare as thumb custom field func. gets custom (default) image
        echo get_bloginfo ( 'template_directory' ); //same as wp-content/themes/your-theme/
        echo '/images/image-pending.gif'; // Put this image into your themes images folder and set the path here
    }
}

现在唯一的问题是<?php echo $thumbnail[0]; ?>如果有多个图像,它会回显所有类似的东西

<img src="    http://applesiam.com/wp-content/uploads/2555-05-02_19h14_34-150x150.png        http://applesiam.com/wp-content/uploads/2555-05-02_19h14_11-150x150.png        http://applesiam.com/wp-content/uploads/2555-05-02_19h13_43-150x123.png        http://applesiam.com/wp-content/uploads/2555-05-02_19h13_20-150x150.png        http://applesiam.com/wp-content/uploads/2555-05-02_19h13_17-150x150.png    ">

正如你所看到的那样,它只是被一些空格隔开了。

如果$thumbnail

中有多个图片,我现在只想拥有最后一张图片

我不是PHP的专家,因为我的PHP课程学期将在下周开始。

提前感谢任何建议。

2 个答案:

答案 0 :(得分:2)

尝试:

$imgSrc = end((explode(' ', trim($imgSrc)));

$imgSrc是您放入<img src="!!!==>>here<<==!!!">的值。

快速输入,不提供任何保修。应保留单个URL完好无损,如果多个空格分隔,则最后一个。

答案 1 :(得分:1)

试试这个。      回声修剪($ thumbnail [sizeof($ thumbnail)-1]);