note 41507 deleted from function.wordwrap by tomsommer
| From: | tomsommer@php.net | Date: | Mon, 12 Apr 2004 23:51:51 +0000 |
| Subject: | note 41507 deleted from function.wordwrap by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-68049@lists.php.net to get a copy of this message | ||
Note Submitter: MC_Breit at phpws.org
----
To split after a specific number of words and get a numeric array, but dont break long words you can
use something like:
<?php
function splitwords($input, $d=1) {
$array = explode(' ', $input);
$n = count($array);
$n = $n/$d;
$return = array();
for($y=0; $y<$n; $y++){
$z = $y;
$z++;
if($y == 0){
$return[1] = implode(' ',array_splice($array, $d));
$return[0] = implode(' ',$array);
}else{
unset($array);
$array = explode(' ', $return[$y]);
$return[$z] = implode(' ',array_splice($array, $d));
$return[$y] = implode(' ',$array);
}
}
sort($return);
for($i=0; $i<count($return); $i++) {
$b = $i;
$b++;
$return[$i] = $return[$b];
}
return $return;
}
$test = 'These are 3 These are too and also those';
$x = 3;
$string = splitwords($test, $x);
for($i=0; $i<count($string); $i++) {
echo $string[$i].'<br>';
}
?>
Returns:
These are 3
These are too
and also those