I am storing tags in my database for a specific item using commas.
For example the tags for a person might be.
$tags = "tall, fun, cool";
I am trying to create each word into a link and remove the comma.
So far I've got,
$tags = str_replace(",", "", $tags);
I would 开发者_开发技巧like the links to be similar to this
<a href="#tall>tall</a> <a href="#fun>fun</a>, etc...
I'm stuck on the creating a link part though, looking for some help please.
foreach (array_map('trim', explode(',', $tags)) as $tag) {
echo "<a href=\"#$tag\">$tag</a>";
}
- Split the string at
,
into an array (explode()
) - Remove all leading and trailing whitespaces (
trim()
) from every array element (array_map()
) - Just iterate and output.
精彩评论