将href包装在implode变量周围

时间:2012-10-12 23:49:55

标签: php syntax implode

我有一个php implode函数来列出wordpress自定义字段键,效果很好。问题是我需要将网址包装在一个href中,以使它们可以点击,并且无法将我的头部包裹在功能sytnax周围。关键值是这种格式www.site.com,所以我设置了一个条目,如下所示:

<?php if(get_post_meta($post->ID, 'website', true)): ?>
    <strong>Website:</strong> <a href="http://<?php echo get_post_meta($post->ID, 'website', true); ?>" target="_blank"><?php echo get_post_meta($post->ID, 'website', true); ?></a>
<?php endif; ?>

但现在我们需要能够保存以逗号分隔的多个条目。以下代码有效,但不输出可点击的网址:

<?php
if( $website = get_post_meta($post->ID, 'website') ):
    $label = count( $website ) > 1 ? 'Websites' : 'Website';
    ?>
    <strong><?php echo $label; ?>:</strong> <?php echo implode( $website, ', ' ); ?><br />
    <?php
endif;
?>

这就是我一直在玩的,这显然是错误的

<?php echo '<a href="http://' . implode('" target="_blank">', $website) . "</a>" . ', '; ?><br />

即使 工作,它也只会输出网址,而不是链接的文字。

--------------------- EDIT -------------------------

Kai的回答最接近,所以我将其标记为答案,但它没有包含label变量。通过嫁给他们中的两个,我想出了这个很漂亮的答案

<?php
    if( $website = get_post_meta($post->ID, 'website') ):
        $label = count( $website ) > 1 ? 'Websites' : 'Website';
        $links = array_map(
            function($url) {
                 $url = htmlspecialchars($url);
                 return sprintf ('<a href="http://%s">%s</a>', $url, $url);
            },
            $website);
        ?>
        <strong><?php echo $label; ?>:</strong> <?php echo implode(', ', $links); ?><br />
<?php endif ?>

3 个答案:

答案 0 :(得分:1)

<?php
if( $website = get_post_meta($post->ID, 'website') ):
    $label = count( $website ) > 1 ? 'Websites' : 'Website';
    ?>
    <strong><?php echo $label; ?>:</strong> 
    <?php foreach($website AS $w): ?>
        <a href="<?php echo $w; ?>" target="_blank"><?php echo $w; ?></a> <br />
    <?php endforeach; ?>      
<?php endif; ?>

这假设您的数组中的每个“网站”都是完整的有效网址,包括http://

我认为问题的根源在于理解implode的作用以及为什么它不会按照你想要的方式运作。

修改

我知道,你希望它们在用逗号分隔的内联列表中。你应该使用Jon的方法,因为它会比我在这里建议的更优雅地做你想做的事。

答案 1 :(得分:0)

您可能会以与您自己尝试过的方式类似的方式严重滥用implode并使其有效,但这确实不是一个好主意。

您想要的是从URL列表移动到锚标记列表,这可以通过array_map实现:

if ($website = get_post_meta($post->ID, 'website')) {
    $links = array_map(
        function($url) {
             $url = htmlspecialchars($url);
             return sprintf ('<a href="http://%s">%s</a>', $url, $url);
        },
        $website);

    echo implode(', ', $links);
}

答案 2 :(得分:0)

您需要使用装饰器模式。修改以下示例以满足您的需求

interface HtmlElementInterface {
    public function getText();
    public function render();
}

class LinkDecorator implements HtmlElementInterface {

    protected $title;
    protected $text;

    public function __construct($text, $title, $renderNow = false) {
        if (!is_string($text) || empty($text)) {
            throw new InvalidArgumentException("The text of the element must be a non-empty string.");
        }
        $this->text = $text;
        $this->title = $title;
        if ($renderNow) {
            echo $this->render();
        }
    }

    public function getText() {
        return $this->text;
    }

    public function render() {
        // do your magic here
        return "<a href='" . $this->text . "'>" . $this->title . "</a>";
    }

}