diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php
index 29706e15341..5d3c209bdc7 100644
--- a/htdocs/core/lib/files.lib.php
+++ b/htdocs/core/lib/files.lib.php
@@ -323,6 +323,7 @@ function dol_dir_is_emtpy($folder)
*
* @param string $file Filename
* @return int <0 if KO, Number of lines in files if OK
+ * @see dol_nboflines
*/
function dol_count_nb_of_line($file)
{
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index a12e4c18ba4..a099b33eee7 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -4488,22 +4488,57 @@ function dol_string_nohtmltag($StringHtml,$removelinefeed=1,$pagecodeto='UTF-8')
* Return first line of text. Cut will depends if content is HTML or not.
*
* @param string $text Input text
+ * @param int $nboflines Nb of lines to get (default is 1 = first line only)
* @return string Output text
* @see dol_nboflines_bis, dol_string_nohtmltag, dol_escape_htmltag
*/
-function dolGetFirstLineOfText($text)
+function dolGetFirstLineOfText($text, $nboflines=1)
{
- if (dol_textishtml($text))
+ if ($nboflines == 1)
{
- $firstline=preg_replace('/
]*>.*$/s','',$text); // The s pattern modifier means the . can match newline characters
- $firstline=preg_replace('/
]*>.*$/s','',$firstline); // The s pattern modifier means the . can match newline characters
+ if (dol_textishtml($text))
+ {
+ $firstline=preg_replace('/
]*>.*$/s','',$text); // The s pattern modifier means the . can match newline characters
+ $firstline=preg_replace('/
]*>.*$/s','',$firstline); // The s pattern modifier means the . can match newline characters
+ }
+ else
+ {
+ $firstline=preg_replace('/[\n\r].*/','',$text);
+ }
+ return $firstline.((strlen($firstline) != strlen($text))?'...':'');
}
else
{
- $firstline=preg_replace('/[\n\r].*/','',$text);
+ $ishtml=0;
+ if (dol_textishtml($text))
+ {
+ $text=preg_replace('/\n/','',$text);
+ $ishtml=1;
+ $repTable = array("\t" => " ", "\n" => " ", "\r" => " ", "\0" => " ", "\x0B" => " ");
+ }
+ else
+ {
+ $repTable = array("\t" => " ", "\n" => "
", "\r" => " ", "\0" => " ", "\x0B" => " ");
+ }
+
+ $text = strtr($text, $repTable);
+ if ($charset == 'UTF-8') { $pattern = '/(
]*>)/Uu'; } // /U is to have UNGREEDY regex to limit to one html tag. /u is for UTF8 support
+ else $pattern = '/(
]*>)/U'; // /U is to have UNGREEDY regex to limit to one html tag.
+ $a = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
+
+ $firstline='';
+ $i=0;
+ $nba = count($a); // 2x nb of lines in $a because $a contains also a line for each new line separator
+ while (($i < $nba) && ($i < ($nboflines * 2)))
+ {
+ if ($i % 2 == 0) $firstline .= $a[$i];
+ elseif (($i < (($nboflines * 2) - 1)) && ($i < ($nba - 1))) $firstline .= ($ishtml?"
\n":"\n");
+ $i++;
+ }
+ unset($a);
+ return $firstline.(($i < $nba)?'...':'');
}
- return $firstline.((strlen($firstline) != strlen($text))?'...':'');
}
@@ -4665,7 +4700,7 @@ function dol_nboflines($s,$maxchar=0)
/**
- * Return nb of lines of a formated text with \n and
(we can't have both \n and br)
+ * Return nb of lines of a formated text with \n and
(WARNING: string must not have mixed \n and br separators)
*
* @param string $text Text
* @param int $maxlinesize Largeur de ligne en caracteres (ou 0 si pas de limite - defaut)
@@ -4701,6 +4736,8 @@ function dol_nboflines_bis($text,$maxlinesize=0,$charset='UTF-8')
}
}
}
+
+ unset($a);
return $nblines;
}
diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php
index 3345d780b7e..814adfb390b 100644
--- a/test/phpunit/FunctionsLibTest.php
+++ b/test/phpunit/FunctionsLibTest.php
@@ -120,10 +120,75 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
+ /**
+ * testDolGetFirstLineOfText
+ *
+ * @return void
+ */
+ public function testDolGetFirstLineOfText()
+ {
+ // Nb of line is same than entry text
+
+ $input="aaaa";
+ $result=dolGetFirstLineOfText($input);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa", $result);
+
+ $input="aaaa\nbbbbbbbbbbbb\n";
+ $result=dolGetFirstLineOfText($input, 2);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa\nbbbbbbbbbbbb", $result);
+
+ $input="aaaa
bbbbbbbbbbbb
";
+ $result=dolGetFirstLineOfText($input, 2);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa
\nbbbbbbbbbbbb", $result);
+
+ // Nb of line is lower
+
+ $input="aaaa\nbbbbbbbbbbbb\ncccccc\n";
+ $result=dolGetFirstLineOfText($input);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa...", $result);
+
+ $input="aaaa
bbbbbbbbbbbb
cccccc
";
+ $result=dolGetFirstLineOfText($input);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa...", $result);
+
+ $input="aaaa\nbbbbbbbbbbbb\ncccccc\n";
+ $result=dolGetFirstLineOfText($input, 2);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa\nbbbbbbbbbbbb...", $result);
+
+ $input="aaaa
bbbbbbbbbbbb
cccccc
";
+ $result=dolGetFirstLineOfText($input, 2);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa
\nbbbbbbbbbbbb...", $result);
+
+ // Nb of line is higher
+
+ $input="aaaa
bbbbbbbbbbbb
cccccc";
+ $result=dolGetFirstLineOfText($input, 100);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa
\nbbbbbbbbbbbb
\ncccccc", $result, 'dolGetFirstLineOfText with nb 100 a');
+
+ $input="aaaa
bbbbbbbbbbbb
cccccc
";
+ $result=dolGetFirstLineOfText($input, 100);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa
\nbbbbbbbbbbbb
\ncccccc", $result, 'dolGetFirstLineOfText with nb 100 b');
+
+ $input="aaaa
bbbbbbbbbbbb
cccccc
\n";
+ $result=dolGetFirstLineOfText($input, 100);
+ print __METHOD__." result=".$result."\n";
+ $this->assertEquals("aaaa
\nbbbbbbbbbbbb
\ncccccc", $result, 'dolGetFirstLineOfText with nb 100 c');
+ }
+
+
/**
* testDolBuildPath
*
- * @return boolean
+ * @return void
*/
public function testDolBuildPath()
{