note 82671 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:27:36 +0000 |
| Subject: | note 82671 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155118@lists.php.net to get a copy of this message | ||
Note Submitter: ivan dot chura at gmail dot com
----
This function can be useful for wrap string by specify chars, not only space char.
<?php
/**
* Wrap string by specify chars
*
* @param string $strForWrap
* @param integer $maxLength
* @param string $breakChar
* @param array $wrapChars
*
* @author Ivan Chura
* @since 21.04.2008
* @return string
**/
function wordwrapBySpecifyChars($strForWrap, $maxLength = 80, $breakChar = "\n",
$wrapChars = array(",", ";" ))
{
$newStr = null;
$length_of_string = strlen($strForWrap);
if ($length_of_string <= $maxLength)
{
return $strForWrap;
}
$count_of_string = 1;
$wait_new_line = false;
for($i=0; $i<$length_of_string; $i++)
{
if ( $count_of_string*$maxLength == $i || $wait_new_line)
{
if (in_array($strForWrap{$i}, $wrapChars ) )
{
$count_of_string ++;
$newStr .= $strForWrap{$i}.$breakChar;
$wait_new_line = false;
}
else
{
$newStr .= $strForWrap{$i};
$wait_new_line = true;
}
}
else
{
$newStr .= $strForWrap{$i};
}
}
return $newStr;
}
?>