Jump to content

Splitting Text


shogemuk

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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