Jump to content

Complicated Counting


lpxxfaintxx

Recommended Posts

Hello,

I have a variable that looks like this:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]0lil alx9d1, BagFullOfRic, YOURMOM, fdndfnfd , fdndfnfd , fdndfnfd, fdndfnfd, fdndfnfd, fdndfnfd, sleeperpick10, BagFullOfRice, BagFullOfRice74, iix3JAD0REuU, iix3JAD0REuU, sinwillsin, sportzr4me18, sleeperpick10, BaSkEtBaLLQT8674, xpjisuuus, BagFullOfShyt, %n, %n, BagFullOfRice, Patelk0014, Patelk0014, BagFullOfRice, BagFullOfRice, , Patelk0014, flirtyazngrl09, bigshot9214, bigshot9214, bigshot9214, mnkmnkz16, bleausky22312, , , fashnFrEEk92, BagFullOfRice, BagFullOfRice, , xbsbdfbdbfdbfd, xbsbdfbdbfdbfd, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, , BagFullOfRice, %n, BagFullOfRice, %n, %n, , BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfShyt, BagFullOfShyt, BagFullOfShyt, rubberducky9314, rubberducky9314, rubberducky9314, %n, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, Patelk0014, rubberducky9314, bigshot9214, bigshot9214, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice[/quote]


Is it possible to count how many of the same names are in there?

For example:

(echoed out text)

0lil alx9d1 (1)
sleeperpick10 (2)
BagFullOfRice (34)
%n (27)
bigshot9214 (3)


Where the number in the parentheses is how many of the same names are in the variable.

Regards,

AIMMultimedia
Link to comment
Share on other sites

Yes, there is....

[code]<?PHP
$var00 = "0lil alx9d1, BagFullOfRic, YOURMOM, fdndfnfd , fdndfnfd , fdndfnfd, fdndfnfd, fdndfnfd, fdndfnfd, sleeperpick10, BagFullOfRice,BagFullOfRice74, iix3JAD0REuU, iix3JAD0REuU, sinwillsin, sportzr4me18, sleeperpick10, BaSkEtBaLLQT8674, xpjisuuus,BagFullOfShyt, %n, %n, BagFullOfRice, Patelk0014, Patelk0014, BagFullOfRice, BagFullOfRice, , Patelk0014, flirtyazngrl09, bigshot9214, bigshot9214, bigshot9214, mnkmnkz16, bleausky22312, , , fashnFrEEk92, BagFullOfRice, BagFullOfRice, , xbsbdfbdbfdbfd, xbsbdfbdbfdbfd, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, , BagFullOfRice, %n, BagFullOfRice, %n, %n, , BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfShyt, BagFullOfShyt, BagFullOfShyt, rubberducky9314, rubberducky9314, rubberducky9314, %n, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, Patelk0014, rubberducky9314, bigshot9214, bigshot9214, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice";

###########
# convert the variable to an array

$var02 = explode(",",$var00);

?>
<pre>
<?PHP

print_r(array_count_values($var02));
?>
</pre>
<?PHP


?>[/code]

note that due to spaces in your variable it will appear that some of the elements are duplicated. to resolve this remove all spaces from the variable BEFORE you convert it to an array

Lite...
Link to comment
Share on other sites

Thanks!, but the results is this:


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Array( [0lil alx9d1] => 1 [ BagFullOfRic] => 1 [ YOURMOM] => 1 [ fdndfnfd ] => 2 [ fdndfnfd] => 4 [ sleeperpick10] => 2 [ BagFullOfRice] => 42 [BagFullOfRice74] => 1 [ iix3JAD0REuU] => 2 [ sinwillsin] => 1 [ sportzr4me18] => 1 [ BaSkEtBaLLQT8674] => 1 [ xpjisuuus] => 1 [BagFullOfShyt] => 1 [ %n] => 13 [ Patelk0014] => 4 [ ] => 6 [ flirtyazngrl09] => 1 [ bigshot9214] => 5 [ mnkmnkz16] => 1 [ bleausky22312] => 1 [ fashnFrEEk92] => 1 [ xbsbdfbdbfdbfd] => 2 [ BagFullOfShyt] => 3 [ rubberducky9314] => 4).
[/quote]

Is there any way I can make it more neat, like


0lil alx9d1 (1) <br>
BagFullOfRic (1) <br>
YOURMOM] (1) <br>
fdndfnfd (2) <br>
fdndfnfd (4) <br>
sleeperpick10 (2) <br>
BagFullOfRice] (42) <br>
BagFullOfRice74] (1) <br>

Or at least:

iix3JAD0REuU] => 2
sinwillsin] => 1
sportzr4me18] => 1
BaSkEtBaLLQT8674] => 1
xpjisuuus] => 1
BagFullOfShyt] => 1



Thanks.
Link to comment
Share on other sites

Surround the print_r() with the '<pre>' & '</pre>' tags:
[code]<?php
$var02 = explode(",",$var00);
echo '<pre>' .  print_r(array_count_values($var02),true) . '</pre>';
?>[/code]

Or you can use a foreach
[code]<?php
$var02 = explode(",",$var00);
foreach ($var02 as $k => $v)
    $var02[$k] = trim($v);
$var04 = array_count_values($var02);

foreach ($var04 as $k => $v)
    echo $k . ' (' . $v . ')<br>';

?>[/code]

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.