Adam1239 Posted August 10, 2007 Share Posted August 10, 2007 Hi, I am quite new to php and i'm looking for a little help with counting how meny times the same number as come up in a string. example: I'm useing a mysql database and the row i want to count reads "2-3-2-2-6". So if i had something that could count how meny times the same number as come up in that row, it would return 3 because the 2 as come up 3 times. there as got to be an easyer way to archive this, other than doing it this way: $p = explode("-",$fetch[$row]); if($p[0] == "2" && $p[2] == "2" && $p[3] == "2"){ etc ... i have been looking all over for a solution but just cant seem to find one. so if you could point me in the right direction it would help alot. Thank you, Adam. Quote Link to comment https://forums.phpfreaks.com/topic/64222-solved-help-with-counting-same-numbers-in-a-string/ Share on other sites More sharing options...
gurroa Posted August 10, 2007 Share Posted August 10, 2007 http://www.php.net/manual/en/function.substr-count.php $test = "2-3-2-2-6"; for ($i = 0; $i < 9; ++$i) echo $i.' is '.substr_count($test, $i).'x times'."<br />\n"; Quote Link to comment https://forums.phpfreaks.com/topic/64222-solved-help-with-counting-same-numbers-in-a-string/#findComment-320177 Share on other sites More sharing options...
Adam1239 Posted August 10, 2007 Author Share Posted August 10, 2007 Hi, gurroa thank you for your reply, the example you gave is just what i was looking for! Quote Link to comment https://forums.phpfreaks.com/topic/64222-solved-help-with-counting-same-numbers-in-a-string/#findComment-320181 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.