在 foreach 中用逗号打印
可能的重复:
删除 PHP 中的最后一个逗号?
所以我有以下代码:
<?php
foreach ($movie->Genres as $genre):
?>
<a href="some_dinamic_link"><?=$genre->name?></a>,
<?php
endforeach;
?>
这会给我以下内容:动作、戏剧、犯罪,但我不需要最后一个逗号分隔符 - 所以我的问题是避免该逗号的最佳方法是什么?
诗。 如果这很重要的话,我的页面上有 4 个像这样的循环 - 针对演员、导演等。
Possible Duplicate:
Removing last comma in PHP?
So I have the following code:
<?php
foreach ($movie->Genres as $genre):
?>
<a href="some_dinamic_link"><?=$genre->name?></a>,
<?php
endforeach;
?>
And this will give me the following: Action, Drama, Crime, but I don't need this last comma separator - so my question is what would be the best way to avoid that comma ?
Ps.
If that matters I have 4 loops like this one on my page - for actors, directors, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 A 标签写入数组并加入项目。
Write you A-tags to an array and join the items.
另一种选择是使用字符串连接,然后使用子字符串来删除结尾的逗号:
The other option is to use string concatenation, then a substring to delete the trailing comma: