The Little Guy Posted April 23, 2009 Share Posted April 23, 2009 consisting of all ones and zeros (1110001101001) what is the best way to add up all of the 1's? Link to comment https://forums.phpfreaks.com/topic/155413-solved-add-numbers-in-string/ Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 substr_count('1110001101001', '1'); Link to comment https://forums.phpfreaks.com/topic/155413-solved-add-numbers-in-string/#findComment-817788 Share on other sites More sharing options...
kenrbnsn Posted April 23, 2009 Share Posted April 23, 2009 Use str_split and array_sum <?php $str = '1110001101001'; echo array_sum(str_split($str)); ?> Ken Link to comment https://forums.phpfreaks.com/topic/155413-solved-add-numbers-in-string/#findComment-817790 Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 Seems like more work if substr_count works perfectly...just tested it. Link to comment https://forums.phpfreaks.com/topic/155413-solved-add-numbers-in-string/#findComment-817792 Share on other sites More sharing options...
The Little Guy Posted April 23, 2009 Author Share Posted April 23, 2009 Thanks! Link to comment https://forums.phpfreaks.com/topic/155413-solved-add-numbers-in-string/#findComment-817803 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.