search

Home  >  Q&A  >  body text

Is there any easy way to break a string into a character array in php?

For example, I want to turn $str = 'adfdf' into an array like ['a', 'd', 'f', 'd', 'f']

伊谢尔伦伊谢尔伦2742 days ago1117

reply all(10)I'll reply

  • PHP中文网

    PHP中文网2017-06-21 10:13:28

    I found that I can use the function str_split

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-21 10:13:28

    I have done string comparison before using the lcs algorithm, which involves breaking up strings into arrays. Due to Chinese issues, it is not possible to use str_split directly. You need to use preg_split

    你可以封装一个公共函数
    
    function mb_str_split($str)
    {
        return preg_split('/(?<!^)(?!$)/u' , $str);
    }

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-21 10:13:28

    $string = '中国共产党万岁!aaaaa';
    var_dump(preg_split('//u', $string, 0, PREG_SPLIT_NO_EMPTY));
    /*
    array (size=13)
      0 => string '中' (length=3)
      1 => string '国' (length=3)
      2 => string '共' (length=3)
      3 => string '产' (length=3)
      4 => string '党' (length=3)
      5 => string '万' (length=3)
      6 => string '岁' (length=3)
      7 => string '!' (length=3)
      8 => string 'a' (length=1)
      9 => string 'a' (length=1)
      10 => string 'a' (length=1)
      11 => string 'a' (length=1)
      12 => string 'a' (length=1)
     */

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-21 10:13:28

    preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-21 10:13:28

    The string itself can be used as an array
    $str = "abcdef";
    You output $str[0].$str[1].$str[2];
    You will find that it is actually $str
    array(" a","b","c","d","e","f");

    reply
    0
  • 学习ing

    学习ing2017-06-21 10:13:28

    $str = 'adfdf';
    $arr = [];
    for($i=0;$i<strlen($str);$i++) {
    $arr[$i] = $str{$i};
    }
    
    var_dump($arr);
    $str = 'adfdf';
    preg_match_all('/(\w{1})/', $str, $matches);
    var_dump($matches[0]);

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-21 10:13:28

    $str = 'adfdf';
    $temp = array();
    for ($i = 0; $i < strlen($str); $i++) {
        array_push($temp, $str{$i});
    }
    var_dump($temp);

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-21 10:13:28

    $str = 'adfdf';

        $len = mb_strlen($str);
        $arr = [];
        if(!isset($str[0])) {
            exit('输入合理字符串!');
        }
        $rb = function ($index = 0) use (&$rb, &$arr, $str, $len) {
             array_push($arr, mb_substr($str, $index, 1,'UTF-8'));
            $index++;
            if ($index == $len) return $arr;
            $rb($index);
        };
        $rb();
        header("Content-type: text/html; charset=UTF-8");
       var_dump($arr);

    reply
    0
  • 代言

    代言2017-06-21 10:13:28

    <?php
    $str = 'adfdf';
    $arr = array();
    for($i=0;$i<strlen($str);$i++) {
    $arr[$i] = $str[$i];
    }
    print_r($arr);
    ?>

    reply
    0
  • 迷茫

    迷茫2017-06-21 10:13:28

    explode doesn’t work?

    reply
    0
  • Cancelreply