代码之家  ›  专栏  ›  技术社区  ›  matthy

PHP:将字符串拆分为数组foreach char

  •  41
  • matthy  · 技术社区  · 17 年前

    我正在考虑将字符串拆分,以丢失字符,然后使用preggmatch计算它是否包含一个大写字母和符号/数字。

        for(var i:uint = 0; i < thisWordCode.length -1 ; i++)
    {
        thisWordCodeVerdeeld[i] = thisWordCode.charAt(i);
        //trace (thisWordCodeVerdeeld[i]);
    }
    

    谢谢

    5 回复  |  直到 17 年前
        1
  •  79
  •   Tom Haigh    17 年前

    $length = strlen($string);
    $thisWordCodeVerdeeld = array();
    for ($i=0; $i<$length; $i++) {
        $thisWordCodeVerdeeld[$i] = $string[$i];
    }
    

    你也可以这样做:

    $thisWordCodeVerdeeld = str_split($string);
    

        2
  •  77
  •   Kiyan    13 年前

    可以使用将字符串转换为数组 str_split 并使用foreach

    $chars = str_split($str);
    foreach($chars as $char){
        // your code
    }
    
        3
  •  15
  •   nneonneo    14 年前

    您可以使用 [] ,就像您对阵列所做的那样:

    $stringLength = strlen($str);
    for ($i = 0; $i < $stringLength; $i++)
        $char = $str[$i];
    
        4
  •  13
  •   Danijel    11 年前

    自从 str_split() preg_split() 具有 u (PCRE_UTF8) 修饰语。

    preg_split( '//u', $str, null, PREG_SPLIT_NO_EMPTY )