将逗号分隔的字符串转换为列表 [英] Convert comma separated string into list

查看:108
本文介绍了将逗号分隔的字符串转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,例如:

I have a string eg:

$string = "word1,word2,word3,word4";

我需要将它回显为< li> 使用PHP的元素。所以 $ string 变成:

I need to echo this into <li> elements using PHP. So $string becomes:

<li>word1</li>
<li>word2</li>
<li>word3</li>
<li>word4</li>


推荐答案

像这样:

Like this:

$string = "word1,word2,word3,word4";
$string = explode(",",$string);
foreach ($string as $str) {
    echo "<li>".$str."</li>";
}

您可以 explode()将该字符串转换为数组,并遍历它,并将结果输出到列表选项中。

You can explode() the string into an array, loop through it, and output the results into a list option.

这篇关于将逗号分隔的字符串转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆