forked from Wavyzz/dolibarr
Merge pull request #16369 from frederic34/doltrunc
replace ... by one char in dol_trunc
This commit is contained in:
@@ -3184,15 +3184,15 @@ function dol_substr($string, $start, $length, $stringencoding = '', $trunconbyte
|
||||
|
||||
|
||||
/**
|
||||
* Truncate a string to a particular length adding '...' if string larger than length.
|
||||
* If length = max length+1, we do no truncate to avoid having just 1 char replaced with '...'.
|
||||
* Truncate a string to a particular length adding '…' if string larger than length.
|
||||
* If length = max length+1, we do no truncate to avoid having just 1 char replaced with '…'.
|
||||
* MAIN_DISABLE_TRUNC=1 can disable all truncings
|
||||
*
|
||||
* @param string $string String to truncate
|
||||
* @param int $size Max string size visible (excluding ...). 0 for no limit. WARNING: Final string size can have 3 more chars (if we added ..., or if size was max+1 or max+2 or max+3 so it does not worse to replace with ...)
|
||||
* @param int $size Max string size visible (excluding …). 0 for no limit. WARNING: Final string size can have 3 more chars (if we added …, or if size was max+1 so it does not worse to replace with ...)
|
||||
* @param string $trunc Where to trunc: 'right', 'left', 'middle' (size must be a 2 power), 'wrap'
|
||||
* @param string $stringencoding Tell what is source string encoding
|
||||
* @param int $nodot Truncation do not add ... after truncation. So it's an exact truncation.
|
||||
* @param int $nodot Truncation do not add … after truncation. So it's an exact truncation.
|
||||
* @param int $display Trunc is used to display data and can be changed for small screen. TODO Remove this param (must be dealt with CSS)
|
||||
* @return string Truncated string. WARNING: length is never higher than $size if $nodot is set, but can be 3 chars higher otherwise.
|
||||
*/
|
||||
@@ -3200,42 +3200,53 @@ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF
|
||||
{
|
||||
global $conf;
|
||||
|
||||
if ($size == 0 || !empty($conf->global->MAIN_DISABLE_TRUNC)) return $string;
|
||||
if ($size == 0 || !empty($conf->global->MAIN_DISABLE_TRUNC)) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
if (empty($stringencoding)) $stringencoding = 'UTF-8';
|
||||
if (empty($stringencoding)) {
|
||||
$stringencoding = 'UTF-8';
|
||||
}
|
||||
// reduce for small screen
|
||||
if ($conf->dol_optimize_smallscreen == 1 && $display == 1) $size = round($size / 3);
|
||||
|
||||
// We go always here
|
||||
if ($trunc == 'right')
|
||||
{
|
||||
if ($trunc == 'right') {
|
||||
$newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string;
|
||||
if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 3))) // If nodot is 0 and size is 1,2 or 3 chars more, we don't trunc and don't add ...
|
||||
return dol_substr($newstring, 0, $size, $stringencoding).($nodot ? '' : '...');
|
||||
else //return 'u'.$size.'-'.$newstring.'-'.dol_strlen($newstring,$stringencoding).'-'.$string;
|
||||
if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 1))) {
|
||||
// If nodot is 0 and size is 1 chars more, we don't trunc and don't add …
|
||||
return dol_substr($newstring, 0, $size, $stringencoding).($nodot ? '' : '…');
|
||||
} else {
|
||||
//return 'u'.$size.'-'.$newstring.'-'.dol_strlen($newstring,$stringencoding).'-'.$string;
|
||||
return $string;
|
||||
} elseif ($trunc == 'middle')
|
||||
{
|
||||
}
|
||||
} elseif ($trunc == 'middle') {
|
||||
$newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string;
|
||||
if (dol_strlen($newstring, $stringencoding) > 2 && dol_strlen($newstring, $stringencoding) > ($size + 1))
|
||||
{
|
||||
if (dol_strlen($newstring, $stringencoding) > 2 && dol_strlen($newstring, $stringencoding) > ($size + 1)) {
|
||||
$size1 = round($size / 2);
|
||||
$size2 = round($size / 2);
|
||||
return dol_substr($newstring, 0, $size1, $stringencoding).'...'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size2, $size2, $stringencoding);
|
||||
} else return $string;
|
||||
} elseif ($trunc == 'left')
|
||||
{
|
||||
return dol_substr($newstring, 0, $size1, $stringencoding).'…'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size2, $size2, $stringencoding);
|
||||
} else {
|
||||
return $string;
|
||||
}
|
||||
} elseif ($trunc == 'left') {
|
||||
$newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string;
|
||||
if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 3))) // If nodot is 0 and size is 1,2 or 3 chars more, we don't trunc and don't add ...
|
||||
return '...'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size, $size, $stringencoding);
|
||||
else return $string;
|
||||
} elseif ($trunc == 'wrap')
|
||||
{
|
||||
if (dol_strlen($newstring, $stringencoding) > ($size + ($nodot ? 0 : 1))) {
|
||||
// If nodot is 0 and size is 1 chars more, we don't trunc and don't add …
|
||||
return '…'.dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size, $size, $stringencoding);
|
||||
} else {
|
||||
return $string;
|
||||
}
|
||||
} elseif ($trunc == 'wrap') {
|
||||
$newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string;
|
||||
if (dol_strlen($newstring, $stringencoding) > ($size + 1))
|
||||
if (dol_strlen($newstring, $stringencoding) > ($size + 1)) {
|
||||
return dol_substr($newstring, 0, $size, $stringencoding)."\n".dol_trunc(dol_substr($newstring, $size, dol_strlen($newstring, $stringencoding) - $size, $stringencoding), $size, $trunc);
|
||||
else return $string;
|
||||
} else return 'BadParam3CallingDolTrunc';
|
||||
} else {
|
||||
return $string;
|
||||
}
|
||||
} else {
|
||||
return 'BadParam3CallingDolTrunc';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -765,36 +765,36 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase
|
||||
*/
|
||||
public function testDolTrunc()
|
||||
{
|
||||
// Default trunc (will add ... if truncation truncation or keep last char if only one char)
|
||||
// Default trunc (will add … if truncation truncation or keep last char if only one char)
|
||||
$input="éeéeéeàa";
|
||||
$after=dol_trunc($input, 3);
|
||||
$this->assertEquals("éeé...", $after, 'Test A1');
|
||||
$this->assertEquals("éeé…", $after, 'Test A1');
|
||||
$after=dol_trunc($input, 2);
|
||||
$this->assertEquals("ée...", $after, 'Test A2');
|
||||
$this->assertEquals("ée…", $after, 'Test A2');
|
||||
$after=dol_trunc($input, 1);
|
||||
$this->assertEquals("é...", $after, 'Test A3');
|
||||
$input="éeéeé";
|
||||
$this->assertEquals("é…", $after, 'Test A3');
|
||||
$input="éeée";
|
||||
$after=dol_trunc($input, 3);
|
||||
$this->assertEquals("éeéeé", $after, 'Test B1');
|
||||
$this->assertEquals("éeée", $after, 'Test B1');
|
||||
$after=dol_trunc($input, 2);
|
||||
$this->assertEquals("éeéeé", $after, 'Test B2');
|
||||
$this->assertEquals("ée…", $after, 'Test B2');
|
||||
$after=dol_trunc($input, 1);
|
||||
$this->assertEquals("é...", $after, 'Test B3');
|
||||
$this->assertEquals("é…", $after, 'Test B3');
|
||||
$input="éeée";
|
||||
$after=dol_trunc($input, 3);
|
||||
$this->assertEquals("éeée", $after, 'Test C1');
|
||||
$after=dol_trunc($input, 2);
|
||||
$this->assertEquals("éeée", $after, 'Test C2');
|
||||
$this->assertEquals("ée…", $after, 'Test C2');
|
||||
$after=dol_trunc($input, 1);
|
||||
$this->assertEquals("éeée", $after, 'Test C3');
|
||||
$this->assertEquals("é…", $after, 'Test C3');
|
||||
$input="éeé";
|
||||
$after=dol_trunc($input, 3);
|
||||
$this->assertEquals("éeé", $after, 'Test C');
|
||||
$after=dol_trunc($input, 2);
|
||||
$this->assertEquals("éeé", $after, 'Test D');
|
||||
$after=dol_trunc($input, 1);
|
||||
$this->assertEquals("éeé", $after, 'Test E');
|
||||
// Trunc with no ...
|
||||
$this->assertEquals("é…", $after, 'Test E');
|
||||
// Trunc with no …
|
||||
$input="éeéeéeàa";
|
||||
$after=dol_trunc($input, 3, 'right', 'UTF-8', 1);
|
||||
$this->assertEquals("éeé", $after, 'Test F');
|
||||
@@ -809,7 +809,7 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase
|
||||
$this->assertEquals("é", $after, 'Test J');
|
||||
$input="éeéeéeàa";
|
||||
$after=dol_trunc($input, 4, 'middle');
|
||||
$this->assertEquals("ée...àa", $after, 'Test K');
|
||||
$this->assertEquals("ée…àa", $after, 'Test K');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user