Jump to content

Count characters of words in an array


Rahul Dev

Recommended Posts

<?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

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 />";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.