PHP Implode wrap in tags

2019-03-11 20:26发布

问题:

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


标签: php html implode