如何将字符添加到字符串中

时间:2015-07-29 02:45:11

标签: php string

我有一个字符串

$str_convert = "'a','b','c','d','e'";

我希望将字符串转换为:

<input type='color' value='transparent'/>

我该怎么办?

3 个答案:

答案 0 :(得分:2)

试试我的解决方案:

<?php
$str = "a,b,c,d,e";
$arr = explode(',',$str);
foreach ($arr as &$value) {
    $value = "'$value'";
}

$str_convert= implode(',', $arr);
echo $str_convert;

答案 1 :(得分:1)

像这样:

$str = "a,b,c,d,e";
$items = split(",", $str);
$convert_str = "";
foreach ($items as $item) {
   $convert_str .= "'$item',";
}
$convert_str = rtrim($convert_str, ",");
print($convert_str);

答案 2 :(得分:1)

如果您希望使用函数式编程编码风格的不同解决方案,那么它是:

<?php
$str = 'a,b,c,d,e';

$add_quotes = function($str, $func) {
    return implode(',', array_map($func, explode(',', $str)));
};


print $add_quotes(
    $str,
    function ($a) {
        return "'$a'";
    }
);
相关问题