字符串多次转换为数组

时间:2019-07-26 03:21:03

标签: php

我想将字符串转换为数组。下面的示例。
1.首字母大写
2.“,”或“ _”后的字母变为大写字母
3.字符串之间用','
隔开 4.将下划线替换为''


    $str_1 = "_ab,cb_ef,kk,uu";
    $str_2 = ",cb_ef,kk,uu";
    $str_3 = "cb_ef,kk,uu";

    //convert to

    $arr_1 = ('Ab','Cb Ef','Kk','Uu');
    $arr_2 = ('Cb Ef','Kk','Uu');
    $arr_3 = ('Cb Ef','Kk','Uu');

    //There are my code and result .

    $func = function( $str ){

        $str_len = strlen( $str );
        for ( $i = 0 ; $i < $str_len ; $i++ )
        {
            if ( $str[ $i ] == ',' && $str[ $i ] == '_' )
            {
                $str[ $i ] = $str[ $i ] == '_' ? ' ' : $str[ $i ] ;
                if ( ord( $str[ $i +1 ] ) <= 122 && ord( $str[ $i +1 ] ) >= 97 )
                {
                    $str[ $i ] = chr( ord( $str[ ++$i ] ) - 32 );
                }
            }
        }
        return $str;
    };
    var_dump(explode( ',' , $func( 'ss,adsa_sdfs' )));

    //result is this

    array(2) { [0]=> string(2) "ss" [1]=> string(9) "adsa_sdfs" }

5 个答案:

答案 0 :(得分:1)

尝试以下代码:

<?php

function ConvertToArr($str)
{
    $result    = array();
    $temp_arr1 = explode(",", $str);
    for ($count1 = 0; $count1 < count($temp_arr1); $count1++) {
        $temp_results = array();
        $temp_arr2    = explode("_", $temp_arr1[$count1]);
        for ($count2 = 0; $count2 < count($temp_arr2); $count2++) {
            $temp_str = ucfirst($temp_arr2[$count2]);
            if ($temp_str != "")    
                $temp_results   []= $temp_str;
        }
        $temp_results = implode(" ", $temp_results);
        if (trim($temp_results) != "")
            $result      []= $temp_results;
    }

    return $result;
}

$str_1 = "_ab,cb_ef,kk,uu";
$str_2 = ",cb_ef,kk,uu";
$str_3 = "cb_ef,kk,uu";

print_r(ConvertToArr($str_1));
print_r(ConvertToArr($str_2));
print_r(ConvertToArr($str_3));

?>

它产生以下输出:

Array
(
    [0] => Ab
    [1] => Cb Ef
    [2] => Kk
    [3] => Uu
)
Array
(
    [0] => Cb Ef
    [1] => Kk
    [2] => Uu
)
Array
(
    [0] => Cb Ef
    [1] => Kk
    [2] => Uu
)

答案 1 :(得分:1)

尝试一下

function conv( $str ) {
    $res = [];
    $arr = explode( ',' , $str );
    foreach ( $arr as $key => $value ) {

        $res[] = ucwords( str_replace( '_' , ' ' , $value ) );

    }
    return $res;
}


var_dump( conv( 'ss,adsa_sdfs' ) );

响应:

array(2) { [0]=> string(2) "Ss" [1]=> string(9) "Adsa Sdfs" }

答案 2 :(得分:1)

带有测试用例的演示代码:

<?php

function upper($s, $sep)
{
    // upper the char separated by $sep
    $a = explode($sep, $s);
    $a = array_map('ucfirst', $a);
    return join($sep, $a);
}

function test($s)
{
    // upper the chars separated by '_'
    $s = upper($s, '_');

    // upper the chars separated by ','
    $s = upper($s, ',');

    // change all '_' to ' ' and upper the chars
    $s = str_replace('_', ' ', $s);
    $s = upper($s, ' ');

    // trim unused chars
    $s = trim($s, ' ,');

    return explode( ',', $s);
}


$tests = [
    "_ab,cb_ef,kk,uu" => ['Ab','Cb Ef','Kk','Uu'],
    ",cb_ef,kk,uu" => ['Cb Ef','Kk','Uu'],
    "cb_ef,kk,uu" => ['Cb Ef','Kk','Uu'],
    "ss,adsa_sdfs" => ['Ss', 'Adsa Sdfs'],
];

foreach ($tests as $s=>$a) {
    $out = test($s);
    if ($out === $a) {
        echo "OK! $s = " . json_encode($a) . PHP_EOL;
    } else {
        echo "WRONG! $s is " . json_encode($out) . PHP_EOL;
    }
}

输出:

OK! _ab,cb_ef,kk,uu = ["Ab","Cb Ef","Kk","Uu"]
OK! ,cb_ef,kk,uu = ["Cb Ef","Kk","Uu"]
OK! cb_ef,kk,uu = ["Cb Ef","Kk","Uu"]
OK! ss,adsa_sdfs = ["Ss","Adsa Sdfs"]

答案 3 :(得分:1)

写这个

<?php 
$str_1 = "_ab,cb_ef,kk,uu";
    $str_2 = ",cb_ef,kk,uu";
    $str_3 = "cb_ef,kk,uu";

    function convert_to($arr){

        $arr = str_replace('_', ' ', $arr); 
        $arr_1 = explode(",",$arr); 

        $new_array = array_map('trim', $arr_1);
        $new_array = array_map('ucwords', $arr_1);  // first letter after word capitalize
        $new_array = array_filter($new_array, 'strlen'); // remove empty item
        $new_array = array_values($new_array); // arrange and shift after removing empty item

        return $new_array;
    }
    $arr_1 = convert_to($str_1);
    $arr_2 = convert_to($str_2);
    $arr_3 = convert_to($str_3);

    var_dump($arr_1);
    var_dump($arr_2);
    var_dump($arr_3);  

输出:

array(4) {
  [0]=>
  string(3) " Ab"
  [1]=>
  string(5) "Cb Ef"
  [2]=>
  string(2) "Kk"
  [3]=>
  string(2) "Uu"
}
array(3) {
  [0]=>
  string(5) "Cb Ef"
  [1]=>
  string(2) "Kk"
  [2]=>
  string(2) "Uu"
}
array(3) {
  [0]=>
  string(5) "Cb Ef"
  [1]=>
  string(2) "Kk"
  [2]=>
  string(2) "Uu"
}

答案 4 :(得分:1)

在这种情况下,您可以使用PHP的内置函数:

function processString($param){
    $explode = explode(',', $param);
    $replace = str_replace('_', ' ', $explode);
    $replace = array_map('trim', $replace);
    $ucfirst = array_map('ucwords', $replace);

    return $ucfirst;
}

解释是:

  1. 首先,我们使用explode()方法基于逗号分隔将字符串转换为数组。
  2. 接下来,我们需要使用str_replace()方法将下划线转换为空格。
  3. 在您的示例中,我注意到其中带空格(以前是下划线)的字符串已被删除。这需要使用trim()来实现。由于字符串已经转换为数组,因此我们使用array_map()方法来处理所有数组值的修剪。
  4. 最后,将每个单词都转换为大写。幸运的是,PHP已经有了方法:ucwords()。只需array_map()

这是测试代码的方式:

$str_1 = "_ab,cb_ef,kk,uu";
$str_2 = ",cb_ef,kk,uu";
$str_3 = "cb_ef,kk,uu";

print_r(processString($str_1));
print_r(processString($str_2));
print_r(processString($str_3));