PHP Implode wrap in tags

…衆ロ難τιáo~ 提交于 2019-12-03 09:08:54

问题


Been trying to google an answer but cant seem to find anything, I have the following...

<?php
    $values =   array_map('trim', get_post_custom_values($key));
    $value  =   implode($values,', ');
    echo "<div class='top-meta-vals'>".apply_filters(" $value\n", $value)."</div>";
?>

I want to wrap each and every $value in a span tag but im unsure how...

I tried,

<?php
$value = "<span>".implode($values,', ')."</span>";
?>

with no luck, can anybody give me an idea of where im going wrong?


回答1:


In this way you are wrapping the entire set in one span, you have to add the closing/opening tag to the implode:

$value = "<span>".implode('</span>,<span>', $values)."</span>";



回答2:


You can use array_map function, smth like this:

$filter = function($tag){ return '<span>' . $tag . '</span>'; };
$spannedTags = array_map($filter, $tags);

End then just implode with ,.




回答3:


Basically, this just implodes your values, using the 'glue' of span closed/open, and wraps it so the first and last items have their beginning/ending spans tags:

$value = "<span>" . implode("</span><span>", $values) . "</span>";


来源:https://stackoverflow.com/questions/9872670/php-implode-wrap-in-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!