简体   繁体   English

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

[英]Convert comma separated string into array

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':我有这个 function 其中 $arg = 'html,css,php,Z81C3B0863CZ,Z81C3B0863CZ,Z81C3B0863CZ,Z81C3B0863CZ,Z81C3B08'0DAD537B72EZE,0987EZA,css,php

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.我们得到逗号和空格,但在最后一个链接之后会有一个尾随逗号。

Just like you explode d you can implode again:就像你explode d 你可以再次内implode

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

return implode(', ', $tags);

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

Try this short code试试这个短代码

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

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

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

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

}

Try this尝试这个

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

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

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

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

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也可以工作: "tag with space,html,css,php,mysql,javascript"

More regex is always good!更多的正则表达式总是好的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM