note 80407 deleted from function.explode by felipe
| From: | felipe@php.net | Date: | Thu, 24 Jan 2008 16:34:21 +0000 |
| Subject: | note 80407 deleted from function.explode by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-135378@lists.php.net to get a copy of this message | ||
Note Submitter: supox0 at walla dot co dot il
----
This will split a long words in a sentence :
<?
function splitLongWords($str,$maxWordLength) {
$arr=explode(' ',$str);
$flag=false;
foreach($arr as $key=>$currentWord)
{
if(strlen($currentWord)>$maxWordLength) {
$currentWord=substr($currentWord,0,$maxWordLength).'
'.substr($currentWord,$maxWordLength);
$arr[$key]=$currentWord;
$flag=true;
}
}
$str=implode(' ',$arr);
if($flag) return splitLongWords($str,$maxWordLength);
else return $str;
}
$str = "this sentence has a too long word : abcdefghijklmnopqrstuvwxyz\n";
echo splitLongWords($str,6);
// Output : this senten ce has too long word : abcdef ghijkl mnopqr stuvwx yz
?>