note 31571 added to function.substr
| From: | loic at NOSPAM dot xhtml dot net | Date: | Sat, 26 Apr 2003 14:31:25 +0000 |
| Subject: | note 31571 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-47140@lists.php.net to get a copy of this message | ||
If you want to substring the middle of a string with another:
/**
* Reduce a string by the middle
*
* ex: http://www.php.net/manual/en/function.str-replace.php
http://www.php.net/manu[...]tion.str-replace.php
* @param string original string
* @param int max size (default 50)
* @param string replacement string (default [...])
* @return string the new string
*/
function PX_strMiddleReduce($string, $max = 50, $rep = '[...]')
{
$strlen = strlen($string);
if ($strlen <= $max) return $string;
$lengthtokeep = $max - strlen($rep);
$mod = $lengthtokeep % 2;
$start = 0;
$end = 0;
if ($mod == 0) {
$start = $lengthtokeep / 2;
$end = $start;
} else {
$start = intval($lengthtokeep / 2);
$end = $start + 1;
}
return substr($string, 0, $start) .$rep. substr($string, -$end);
}
echo "<pre>";
echo "1---5----10---15---20---25---30---35";
echo "---40---45---50---55---60---65---70---75---80\n";
echo PX_strMiddleReduce( 'http://www.php.net/manual/en/function.substr.php',
50, '[...]');
echo "\n";
echo PX_strMiddleReduce( 'http://www.php.net/manual/en/function.substr.php',
30, '...');
echo "\n";
echo PX_strMiddleReduce( 'http://www.php.net/manual/en/function.substr.php',
30, '[...]');
echo "\n";
echo PX_strMiddleReduce( 'http://www3.php.net/manual/en/function.substr.php',
30, '[...]');
echo "\n";
echo "</pre>";
--
http://www.php.net/manual/en/function.substr.php
http://master.php.net/manage/user-notes.php?action=edit+31571
http://master.php.net/manage/user-notes.php?action=delete+31571
http://master.php.net/manage/user-notes.php?action=reject+31571