note 31571 modified in function.substr by aidan
| From: | aidan@php.net | Date: | Sat, 03 Jul 2004 07:46:08 +0000 |
| Subject: | note 31571 modified in function.substr by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72369@lists.php.net to get a copy of this message | ||
If you want to substring the middle of a string with another:
<?php
/**
* 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>";
?>
--was--
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://php.net/manual/en/function.substr.php