将逗号分隔的字符串转换为数组

时间:2022-02-19 21:27:41

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.

我们得到逗号和空格但在最后一个链接后会有一个尾随逗号。

10 个解决方案

#1


14  

Just like you exploded you can implode again:

就像你爆炸一样,你可以再次崩溃:

$tags = explode(',', $arg);
foreach ($tags as &$tag) {
    $tag = '<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>';
}

return implode(', ', $tags);

#2


2  

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);
} 

#3


1  

Try this short code

试试这个简短的代码

$string = 'html,css,php,mysql,javascript';

$ string ='html,css,php,mysql,javascript';

$string = explode(',', $string);
   foreach( $string as $link){
   echo '<a href="tag/'.$link.'">'.$link.'</a>';
}

$ string = explode(',',$ string); foreach($ string as $ link){echo' '。$ link。''; }

#4


0  

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);

}

#5


0  

Try this

尝试这个

$tagss = trim($tagss);    
return substr($tagss, 0 , strlen($tagss)-1);

#6


0  

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);

#7


0  

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而不是explode,它也可以更好地处理输入空间。

#8


0  

function info_get_tags($arg)
{
    global $u;
    if (empty($arg)) return '';
    return ltrim(preg_replace('/([^\,]+)/', ' <a href="' . $u . '/${1}/" title="${1}">${1}</a>', $arg));
}

#9


0  

$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>

#10


-2  

Another solution:

另一种方案:

$html = trim(preg_replace('/([^,]+)/', ' <a href="/tags/\1" title="\1">\1</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也可以工作:“用空格,html,css,php,mysql,javascript标记”

More regex is always good!

更多正则表达总是好的!

#1


14  

Just like you exploded you can implode again:

就像你爆炸一样,你可以再次崩溃:

$tags = explode(',', $arg);
foreach ($tags as &$tag) {
    $tag = '<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>';
}

return implode(', ', $tags);

#2


2  

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);
} 

#3


1  

Try this short code

试试这个简短的代码

$string = 'html,css,php,mysql,javascript';

$ string ='html,css,php,mysql,javascript';

$string = explode(',', $string);
   foreach( $string as $link){
   echo '<a href="tag/'.$link.'">'.$link.'</a>';
}

$ string = explode(',',$ string); foreach($ string as $ link){echo' '。$ link。''; }

#4


0  

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);

}

#5


0  

Try this

尝试这个

$tagss = trim($tagss);    
return substr($tagss, 0 , strlen($tagss)-1);

#6


0  

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);

#7


0  

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而不是explode,它也可以更好地处理输入空间。

#8


0  

function info_get_tags($arg)
{
    global $u;
    if (empty($arg)) return '';
    return ltrim(preg_replace('/([^\,]+)/', ' <a href="' . $u . '/${1}/" title="${1}">${1}</a>', $arg));
}

#9


0  

$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>

#10


-2  

Another solution:

另一种方案:

$html = trim(preg_replace('/([^,]+)/', ' <a href="/tags/\1" title="\1">\1</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也可以工作:“用空格,html,css,php,mysql,javascript标记”

More regex is always good!

更多正则表达总是好的!