开发者

Creating links with words in a string

开发者 https://www.devze.com 2023-03-07 17:22 出处:网络
I am storing tags in my database for a specific item using commas. For example the tags for a person might be.

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>";
}
  1. Split the string at , into an array (explode())
  2. Remove all leading and trailing whitespaces (trim()) from every array element (array_map())
  3. Just iterate and output.
0

精彩评论

暂无评论...
验证码 换一张
取 消