SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Dec 1, 2008, 11:31 #1
- 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.
-
Dec 1, 2008, 11:36 #2
http://us.php.net/manual/en/function.str-split.php
PHP Code:$arr = str_split( 'string goes here...', 8 );
PHP Code:function something ( $str )
{
$str = str_split( $str );
return array_count_values( $str );
}
$arr = array_map( 'something', str_split( 'ABAAABAEEEAADDHJKLRTYORGJKLOP', 8 ) );
var_dump( $arr );
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) }
-
Dec 1, 2008, 13:52 #3
- Join Date
- Dec 2008
- Posts
- 18
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks logic_earth.
-
Dec 1, 2008, 14:45 #4
- 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 ...
-
Dec 1, 2008, 14:48 #5
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.
Bookmarks