dpvhv66448 2017-03-21 09:17
浏览 104

为什么要在php中使用str_split()?

Given the following code :

$str = 'CLAX';
echo $str[2];  //prints 'A'

then why should I use str_split( $str ) to convert string to a array of characters ?

I understand str_split( $str , 2 ) will return array of strings; each string being 2 characters long.

http://php.net/manual/en/function.str-split.php

  • 写回答

2条回答 默认 最新

  • doushu2699 2017-03-21 09:21
    关注

    This function is to split a string into an array with given string split length

    By default string split length is set 1

    If you want to split a string into given in given length, then you can use str_split. But in your case you are splitting string with default length 1 that is by you are getting confused.

    <?php
    
    $str = "CLAX";
    echo $str[2]; //here you are referring to 2 index of string
    
    $arr2 = str_split($str);
    Array
    (
        [0] => C
        [1] => L
        [2] => A
        [3] => X
    )
    echo $str[2]; //here you are referring to 2 index of an array
    

    str_split reference

    <?php
    
    $str = "Hello Friend";
    $arr2 = str_split($str, 3);
    Array
    (
        [0] => Hel
        [1] => lo
        [2] => Fri
        [3] => end
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?