Rahul Dev Posted December 9, 2010 Share Posted December 9, 2010 <?php $title = "This is an example of a sentence in a paragraph"; $title_array = explode(" ", $title); $count = strlen($title_array[0]); ?> what i want to do is store the count for every characters of every word in the array using a loop e.g the count will be stored in a variable $chr_count then when i access the count for "This" it should be like this $chr_count[0]=4, $char_count[1]=2 etc. Link to comment https://forums.phpfreaks.com/topic/221100-count-characters-of-words-in-an-array/ Share on other sites More sharing options...
marcus Posted December 9, 2010 Share Posted December 9, 2010 $chr_count = array(); foreach($title_array AS $word){ $chr_count[] = strlen($word); } Link to comment https://forums.phpfreaks.com/topic/221100-count-characters-of-words-in-an-array/#findComment-1144822 Share on other sites More sharing options...
phprocker Posted December 9, 2010 Share Posted December 9, 2010 It's late and I'm tired so hopefully I did this right. I think this is what you mean. $title = "This is an example of a sentence in a paragraph"; $title_array = explode(" ", $title); foreach($title_array as $value) { $count = strlen($value); echo "The word has " . $count . " characters.<br />"; } Link to comment https://forums.phpfreaks.com/topic/221100-count-characters-of-words-in-an-array/#findComment-1144824 Share on other sites More sharing options...
Rahul Dev Posted December 9, 2010 Author Share Posted December 9, 2010 $chr_count = array(); foreach($title_array AS $word){ $chr_count[] = strlen($word); } Thanx for the help! Link to comment https://forums.phpfreaks.com/topic/221100-count-characters-of-words-in-an-array/#findComment-1144825 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.