php 将逗号分隔的字符串转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5845121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert comma separated string into array
提问by med
I have a comma separated string, which consists of a list of tags and want to convert it to array to get a link for every tag.
我有一个逗号分隔的字符串,它由一个标签列表组成,并希望将其转换为数组以获取每个标签的链接。
Example:
例子:
$string = 'html,css,php,mysql,javascript';
I want to make it like this:
我想让它像这样:
<a href="tag/html">html</a>, <a href="tag/css">css</a>, <a href="tag/php">php</a>, <a href="tag/mysql">mysql</a>, <a href="tag/javascript">javascript</a>
So the result will be a string containing comma separated links with a space after each link and with no comma after the last link.
所以结果将是一个包含逗号分隔链接的字符串,每个链接后有一个空格,最后一个链接后没有逗号。
I have this function where $arg = 'html,css,php,mysql,javascript':
我有这个函数,其中 $arg = 'html,css,php,mysql,javascript':
function info_get_tags( $arg ) {
global $u;
$tagss = '';
if ( $arg == '' ) {
return '';
} else {
$tags_arr = explode( ',' , $arg );
foreach ( $tags_arr as $tag ) {
$tags = '<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>';
$tagss .= $tags;
}
return $tagss;
}
}
This script works for me but without commas and spaces and if we add a comma and a space here:
这个脚本对我有用,但没有逗号和空格,如果我们在这里添加一个逗号和一个空格:
$tags = '<a href="' . $u . 'tag/' . $tag . '/">' . $tag . '</a>, ';
we get commas and spaces but there will be a trailing comma after the last link.
我们得到逗号和空格,但最后一个链接后面会有一个逗号。
回答by NikiC
回答by Nick
Here's an alternative that uses array_map instead of the foreach loop:
这是使用 array_map 而不是 foreach 循环的替代方法:
global $u;
function add_html($tag){
return('<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>');
}
function get_tags($taglist){
$tags_arr = array_map("add_html", explode( ',' , $taglist));
return implode(", " , $tags_arr);
}
回答by Cross
Try this short code
试试这个短代码
$string = 'html,css,php,mysql,javascript';
$string = explode(',', $string);
foreach( $string as $link){
echo'<a href="tag/'.$link.'">'.$link.'</a>';
}
$string = 'html,css,php,mysql,javascript';
$string =explode(',', $string);
foreach( $string as $link){
echo'<a href="tag/'.$link.'">'.$link.'</a>';
}
回答by Alphons
$string = 'html,css,php,mysql,javascript';
puts $string.split(/,/).map { |tag| "<a href=\"tag/#{tag}\">#{tag}</a>"}.join(", ")
result:
结果:
<a href="tag/html">html</a>, <a href="tag/css">css</a>, <a href="tag/php">php</a>, <a href="tag/mysql">mysql</a>, <a href="tag/javascript">javascript</a>
回答by prodigitalson
Easiest way is to the html into an array (each tag link is an array element) and then implode on ,
...
最简单的方法是将 html 放入一个数组中(每个标签链接都是一个数组元素),然后在,
...
if ( $arg == '' ) {
return '';
} else {
$tags_arr = explode( ',' , $arg );
$tags = array();
$tagtpl = '<a href="%s" title="%s">%s</a>';
foreach ( $tags_arr as $tag ) {
$url = $u . 'tag/' . $tag . '/';
$tags[] = sprintf($tagtpl, $url, $tag, $tag);
}
return implode(', ', $tags);
}
回答by Igor
Try this
尝试这个
$tagss = trim($tagss);
return substr($tagss, 0 , strlen($tagss)-1);
回答by Arjan
Why not put all tags in an array when you are creating them, and later explode the array and add the commas and spaces between the tags.
为什么不在创建时将所有标签放在一个数组中,然后分解数组并在标签之间添加逗号和空格。
foreach ( $tags_arr as $tag ) {
$tags = '<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>';
$tagss[] = $tags;
}
$tagss = explode(', ', $tagss);
回答by mario
The workaround(!) would be to remove the unwanted trailing characters afterwards:
该解决方法(!),将是以后删除不需要的尾部字符:
$tagss = rtrim($tagss, ", ");
This rtrim
removes any mix of spaces and commas from the right end of the string.
这rtrim
将从字符串的右端删除任何空格和逗号的混合。
Btw, you could use str_getcsv
instead of explode, which also handles input spaces better.
顺便说一句,您可以使用str_getcsv
而不是爆炸,它也可以更好地处理输入空间。
回答by Narf
function info_get_tags($arg)
{
global $u;
if (empty($arg)) return '';
return ltrim(preg_replace('/([^\,]+)/', ' <a href="' . $u . '//" title=""></a>', $arg));
}
回答by Rudie
Another solution:
另一种解决方案:
$html = trim(preg_replace('/([^,]+)/', ' <a href="/tags/" title=""></a>', $string));
Or if you have to html encode the tags (always smart, since you're creating html from text):
或者,如果您必须对标签进行 html 编码(总是很聪明,因为您是从文本创建 html 的):
$html = trim(preg_replace_callback('/([^,]+)/', function($match) {
$tag = $match[1];
return ' <a href="/tags/' . urlencode($tag) . '" title="' . htmlspecialchars($tag) . '">' . htmlspecialchars($tag) . '</a>';
}, $string));
So this $string
would work too: "tag with space,html,css,php,mysql,javascript"
所以这$string
也可以:"tag with space,html,css,php,mysql,javascript"
More regex is always good!
更多的正则表达式总是好的!