SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Separate each 8 character in array

    Hi…
    I have a string (contain A-Z), I want separate each 8 character of this string to array. And next count each word in each batches. For example :

    String is (in this case I separate each 8 character with space [just for description my sample] but in real there aren’t spaces and all characters are proximate:

    $string = "ABAAABAE EEAADDHJ KLRTYORG JKLOP"


    Output:
    Array
    (
    [0] => ABAAABAE
    [1] => EEAADDHJ
    [2] => KLRTYORG
    [3] => JKLOP
    )

    And then print:
    In first 8 characters: A=5, B=2, E=1
    In second 8 characters: E=2, A=2, D=2, H=1, J=1
    In third 8 characters: K=1, L=1, R=1 T=1, Y=1, O=1, R=1, G=1
    And …


    I think 'strlen' - 'strpos' - 'substr' functions can be useful and with loops and for structure it will made. But I’m confused. Please help me… thanks.

  2. #2
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    http://us.php.net/manual/en/function.str-split.php
    PHP Code:
    $arr str_split'string goes here...'); 
    Then... http://us.php.net/manual/en/function...unt-values.php

    PHP Code:
    function something $str )
    {
        
    $str str_split$str );
        return 
    array_count_values$str );
    }

    $arr array_map'something'str_split'ABAAABAEEEAADDHJKLRTYORGJKLOP') );
    var_dump$arr ); 
    Returns:
    Code:
    array(4) {
      [0]=>
      array(3) {
        ["A"]=>
        int(5)
        ["B"]=>
        int(2)
        ["E"]=>
        int(1)
      }
      [1]=>
      array(5) {
        ["E"]=>
        int(2)
        ["A"]=>
        int(2)
        ["D"]=>
        int(2)
        ["H"]=>
        int(1)
        ["J"]=>
        int(1)
      }
      [2]=>
      array(7) {
        ["K"]=>
        int(1)
        ["L"]=>
        int(1)
        ["R"]=>
        int(2)
        ["T"]=>
        int(1)
        ["Y"]=>
        int(1)
        ["O"]=>
        int(1)
        ["G"]=>
        int(1)
      }
      [3]=>
      array(6) {
        ["J"]=>
        int(1)
        ["K"]=>
        int(1)
        ["L"]=>
        int(1)
        ["O"]=>
        int(1)
        ["P"]=>
        int(1)
      }
    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks logic_earth.

  4. #4
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry...
    how i can print these array with foreach?! I'm confused again ...

  5. #5
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well with some basic knowledge of how foreach works and inspecting the structure of the array, you should be able to come up with a solution.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •