Need to create a character map of a long string with php -
i need create character map long string, need each character , position in string, characters repeating many times need each position have appeared in string. thought lot didn't idea. below example string:
"/9j/4aaqskzjrgabageayabgaad/4q8hrxhpzgaatu0akgaaaagabgeyaaiaaaauaaaavkdgaamaaaabaamaaedjaamaaaabadiaajydaaeaaaaoaaaaaoocaacaaaf0aa+fhgkddfevbbghhhghhhhgskfaaaaaaabbbbbbbbbbb===bbhjstdef"
sounds searching this:
$string = "/9j/4aaqskzjrgabageayabgaad/4q8hrxhpzgaatu0akgaaaagabgeyaaiaaaauaaaavkdgaamaaaabaamaaedjaamaaaabadiaajydaaeaaaaoaaaaaoocaacaaaf0aa+fhgkddfevbbghhhghhhhgskfaaaaaaabbbbbbbbbbb===bbhjstdef"; $positions = array(); for($i=0;$i<strlen($string);$i++) { $char = $string[$i]; if(!isset($positions[$char])) { $positions[$char] = array(); } $positions[$char][] = $i; } // example output foreach($positions $key => $val) { printf("%s occurs @ %s\n", $key, implode(',', $val)); }
output:
a occurs @ 5,6,14,16,19,21,24,25,38,39,43,46,47,48,49,51,56,57,59,60,61,62,64,65,66,67,72,73,75,76,77,78,80,81,83,84,88,89,91,92,93,94,96,99,100,104,105,107,108,109,110,112,113,114,115,116,120,121,123,124,125,128,129,155,156,160,161 b occurs @ 15,22,52,79,95,162,163,164,165,166,167,168,169,170,171,172 d occurs @ 26,97 e occurs @ 18,54,85,106 0 occurs @ 42,127 + occurs @ 130 / occurs @ 0,3,27 = occurs @ 173,174,175 g occurs @ 71 h occurs @ 31 occurs @ 58,98 j occurs @ 11,87,101 k occurs @ 44 m occurs @ 74,82,90 o occurs @ 111,117 q occurs @ 7,29 r occurs @ 12,32 s occurs @ 8 t occurs @ 40 u occurs @ 41,63 v occurs @ 68 x occurs @ 33 y occurs @ 20 z occurs @ 10,36 occurs @ 157,158,159 b occurs @ 140,141,176,177 c occurs @ 119,122 d occurs @ 70,86,103,135,136,182 e occurs @ 138,183 f occurs @ 126,131,137,154,184 g occurs @ 13,17,23,37,45,50,53,133,142,146,151 h occurs @ 34,132,143,144,145,147,148,149,150,178 j occurs @ 2,179 k occurs @ 9,69,134,153 o occurs @ 118 p occurs @ 35 s occurs @ 152,180 t occurs @ 181 v occurs @ 139 y occurs @ 55,102 4 occurs @ 4,28 8 occurs @ 30 9 occurs @ 1
Comments
Post a Comment