note 41507 added to function.wordwrap
| From: | MC_Breitatphpws dot org at rn2 dot php dot net | Date: | Mon, 12 Apr 2004 23:32:05 +0000 |
| Subject: | note 41507 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-68048@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41507
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41507&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41507&report=yes
Search -- http://master.php.net/manage/user-notes.php