shogemuk Posted March 12, 2012 Share Posted March 12, 2012 Hi Guys, does anyone know how split a text into groups depending on how many words are in the text e.g $text = "If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. "; I want a maximium of 5 words in each group, so the above text will be split as such $text1 = 'If you are new to'; $text2 = 'PHP and want to get'; $text3 = 'some idea of how it '; $text4 = 'works, try the introductory tutorial.'; But if I want a maximium of 10 words in each group, so the above text will then be split like this $text1 = 'If you are new to PHP and want to get'; $text2 = 'some idea of how it works, try the introductory tutorial.'; $text3 = ''; $text4 = ''; Is this possible? Please any help will be much appreciated thanks Link to comment https://forums.phpfreaks.com/topic/258755-splitting-text/ Share on other sites More sharing options...
dragon_sa Posted March 12, 2012 Share Posted March 12, 2012 from php.net http://www.php.net/manual/en/function.preg-split.php <?php /** * Split a string into groups of words with a line no longer than $max * characters. * * @param string $string * @param integer $max * @return array **/ function split_words($string, $max = 1) { $words = preg_split('/\s/', $string); $lines = array(); $line = ''; foreach ($words as $k => $word) { $length = strlen($line . ' ' . $word); if ($length <= $max) { $line .= ' ' . $word; } else if ($length > $max) { if (!empty($line)) $lines[] = trim($line); $line = $word; } else { $lines[] = trim($line) . ' ' . $word; $line = ''; } } $lines[] = ($line = trim($line)) ? $line : $word; return $lines; } ?> Link to comment https://forums.phpfreaks.com/topic/258755-splitting-text/#findComment-1326479 Share on other sites More sharing options...
dragon_sa Posted March 12, 2012 Share Posted March 12, 2012 sorry I misread that the $max is the length in characters not words Link to comment https://forums.phpfreaks.com/topic/258755-splitting-text/#findComment-1326483 Share on other sites More sharing options...
dragon_sa Posted March 12, 2012 Share Posted March 12, 2012 I just put this together and it works <?php $test = "this is a black dog walking down the road, he eats some food, he was hungry now he sleeps and has a dream of cats running in the backyard, what a crazy dog"; $parts = explode(" ", $test); $i=0; $k=0; $num = array(); foreach ($parts as $part) { if ($i == 5) { $i = 0; $k++; } $num[$k] .= "$part "; $i++; } foreach ($num as $res) { echo "$res<br/>"; } ?> Link to comment https://forums.phpfreaks.com/topic/258755-splitting-text/#findComment-1326487 Share on other sites More sharing options...
shogemuk Posted March 12, 2012 Author Share Posted March 12, 2012 dragon_sa thank you so much, that has solved my problem. Thanks a lot Link to comment https://forums.phpfreaks.com/topic/258755-splitting-text/#findComment-1326489 Share on other sites More sharing options...
dragon_sa Posted March 12, 2012 Share Posted March 12, 2012 here it is again as a function <?php $test = "this is a black dog walking down the road, he eats some food, he was hungry now he sleeps and has a dream of cats running in the backyard, what a crazy dog"; function wordSplit ($word,$count) { $parts = explode(" ", $word); $i=0; $k=0; $num = array(); foreach ($parts as $part) { if ($i == $count) { $i = 0; $k++; } $num[$k] .= "$part "; $i++; } foreach ($num as $res) { $result .= "$res<br/>"; } return $result; } echo wordSplit($test,5); ?> This allows you to throw any string at it and set the number of words to split by Link to comment https://forums.phpfreaks.com/topic/258755-splitting-text/#findComment-1326492 Share on other sites More sharing options...
shogemuk Posted March 12, 2012 Author Share Posted March 12, 2012 Its like you read my mind , Thank you again. Link to comment https://forums.phpfreaks.com/topic/258755-splitting-text/#findComment-1326500 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.