note 49258 deleted from ref.strings by derick
| From: | derick@php.net | Date: | Wed, 09 Feb 2005 18:09:26 +0000 |
| Subject: | note 49258 deleted from ref.strings by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-84735@lists.php.net to get a copy of this message | ||
Note Submitter: carlosreche at yahoo dot com
----
This function gets a substring without cutting words in the middle. You can reduce the main string
by the left, right (default), both or by the center. I think the parameters are self-explicative. :)
Regards,
Carlos
<?php
define("STR_REDUCE_LEFT", 1);
define("STR_REDUCE_RIGHT", 2);
define("STR_REDUCE_CENTER", 4);
function str_reduce($str, $max_length, $append = NULL, $position = STR_REDUCE_RIGHT,
$remove_extra_spaces = true)
{
if ((!is_string($str)) || (!is_int($max_length)) || (!is_string($append) && $append
!== NULL) || (!is_int($position)) ||
(($position != STR_REDUCE_LEFT) && ($position != STR_REDUCE_RIGHT) &&
($position != STR_REDUCE_CENTER) && ($position != (STR_REDUCE_LEFT |
STR_REDUCE_RIGHT)))
)
{
echo "<br /><strong>Warning</strong>: Bad arguments for " .
__FUNCTION__ . "().";
return false;
}
if ($append === NULL)
{
$append = "...";
}
$str = html_entity_decode($str);
if ((bool)$remove_extra_spaces)
{
$str = preg_replace("/\s+/s", " ", trim($str));
}
if (strlen($str) <= $max_length)
{
return html_entities($str);
}
if ($position == STR_REDUCE_LEFT)
{
$str_reduced = preg_replace("/^.*?(\s.{0," . $max_length . "})$/s",
"\\1", $str);
while ((strlen($str_reduced) + strlen($append)) > $max_length)
{
$str_reduced = preg_replace("/^\s?[^\s]+(\s.*)$/s", "\\1",
$str_reduced);
}
$str_reduced = $append . $str_reduced;
}
else if ($position == STR_REDUCE_RIGHT)
{
$str_reduced = preg_replace("/^(.{0," . $max_length . "}\s).*?$/s",
"\\1", $str);
while ((strlen($str_reduced) + strlen($append)) > $max_length)
{
$str_reduced = preg_replace("/^(.*?\s)[^\s]+\s?$/s", "\\1",
$str_reduced);
}
$str_reduced .= $append;
}
else if ($position == (STR_REDUCE_LEFT | STR_REDUCE_RIGHT))
{
$offset = ceil((strlen($str) - $max_length) / 2);
$str_reduced = preg_replace("/^.{0," . $offset . "}|.{0," . $offset .
"}$/s", "", $str);
$str_reduced = preg_replace("/^[^\s]+|[^\s]+$/s", "", $str_reduced);
while ((strlen($str_reduced) + (2 * strlen($append))) > $max_length)
{
$str_reduced = preg_replace("/^(.*?\s)[^\s]+\s?$/s", "\\1",
$str_reduced);
if ((strlen($str_reduced) + (2 * strlen($append))) > $max_length)
{
$str_reduced = preg_replace("/^\s?[^\s]+(\s.*)$/s", "\\1",
$str_reduced);
}
}
$str_reduced = $append . $str_reduced . $append;
}
else if ($position == STR_REDUCE_CENTER)
{
$pattern = "/^(.{0," . floor($max_length / 2) . "}\s)|(\s.{0," .
floor($max_length / 2) . "})$/s";
preg_match_all($pattern, $str, $matches);
$begin_chunk = $matches[0][0];
$end_chunk = $matches[0][1];
while ((strlen($begin_chunk) + strlen($append) + strlen($end_chunk)) > $max_length)
{
$end_chunk = preg_replace("/^\s?[^\s]+(\s.*)$/s", "\\1",
$end_chunk);
if ((strlen($begin_chunk) + strlen($append) + strlen($end_chunk)) > $max_length)
{
$begin_chunk = preg_replace("/^(.*?\s)[^\s]+\s?$/s", "\\1",
$begin_chunk);
}
}
$str_reduced = $begin_chunk . $append . $end_chunk;
}
return htmlentities($str_reduced);
}
?>