Php 围绕内爆变量包装href

Php 围绕内爆变量包装href,php,syntax,implode,Php,Syntax,Implode,我有一个php内爆函数来列出wordpress自定义字段键,这非常有用。问题是我需要将web URL包装在href中,以使其可点击,而不能将我的头脑缠绕在函数sytnax上。键值的格式为www.site.com,因此我将其设置为单个条目,如下所示: <?php if(get_post_meta($post->ID, 'website', true)): ?> <strong>Website:</strong> <a href="http:/

我有一个php内爆函数来列出wordpress自定义字段键,这非常有用。问题是我需要将web URL包装在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; ?>

网站:
但是现在我们需要能够保存多个条目,它们之间用逗号分隔。以下代码有效,但不输出可单击的url:

<?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 />

你可能会严重滥用
内爆
,其方式与你自己尝试过的方式类似,并使其发挥作用,但这确实不是一个好主意

您需要的是从URL列表移动到锚定标记列表,这可以通过以下方式实现:

if($website=get\u post\u meta($post->ID,'website')){
$links=数组\映射(
函数($url){
$url=htmlspecialchars($url);
返回sprintf(“”,$url,$url);
},
$网站);
回波内爆(“,”,$links);
}



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

我认为问题的根源在于理解什么能起作用以及为什么不能按你想要的方式工作

编辑:


我明白了,你希望他们在一个用逗号分隔的内联列表中。那么你应该使用Jon的方法,因为它会比我在这里建议的更优雅地做你想做的事情。

你需要使用装饰图案。修改下面的示例以满足您的需要

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>";
    }

}
接口HTMLElementerface{
公共函数getText();
公共功能呈现();
}
类LinkDecorator实现HtmlElementInterface{
受保护的产权;
受保护的文本;
公共函数构造($text、$title、$renderNow=false){
如果(!is_string($text)| | empty($text)){
抛出新的InvalidArgumentException(“元素的文本必须是非空字符串”);
}
$this->text=$text;
$this->title=$title;
如果($renderNow){
echo$this->render();
}
}
公共函数getText(){
返回$this->text;
}
公共职能{
//在这里施展你的魔法
返回“”;
}
}

这将返回值,但不考虑逗号。。。如果我在函数中添加逗号,它会将其添加到每个url,包括最后一个url。你没有理解内爆是对的。我是勒宁!我在一个代码编辑器中,所以它不会“说”任何东西,它只是旁边有一个警告颜色,下面代码的所有颜色都会改变。我试着在最后一个花括号中加上分号,但它也不喜欢这样。你介意把它编辑成包含
吗?我遇到的问题是,在这之后有一个
,显然这是不需要的。这个解决方案非常棒,但是它省略了标签
网站:
这是最接近的,所以我将其标记为答案,但是因为它没有包含标签变量,所以我编辑了原始问题以包含最终解决方案。
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);
}
<?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; ?>
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>";
    }

}