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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b