mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Stop goofy formatting when newlines are present already.
This commit is contained in:
parent
f6975f5254
commit
389f745d16
2 changed files with 35 additions and 1 deletions
|
@ -377,6 +377,23 @@ TEXT;
|
||||||
$this->assertTextEquals($expected, $result, 'Text not wrapped.');
|
$this->assertTextEquals($expected, $result, 'Text not wrapped.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that wordWrap() properly handle newline characters.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testWordWrapNewlineAware() {
|
||||||
|
$text = 'This is a line that is almost the 55 chars long.
|
||||||
|
This is a new sentence which is manually newlined, but is so long it needs two lines.';
|
||||||
|
$result = String::wordWrap($text, 55);
|
||||||
|
$expected = <<<TEXT
|
||||||
|
This is a line that is almost the 55 chars long.
|
||||||
|
This is a new sentence which is manually newlined, but
|
||||||
|
is so long it needs two lines.
|
||||||
|
TEXT;
|
||||||
|
$this->assertTextEquals($expected, $result, 'Text not wrapped.');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test wrap method.
|
* test wrap method.
|
||||||
*
|
*
|
||||||
|
|
|
@ -346,7 +346,7 @@ class String {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unicode aware version of wordwrap.
|
* Unicode and newline aware version of wordwrap.
|
||||||
*
|
*
|
||||||
* @param string $text The text to format.
|
* @param string $text The text to format.
|
||||||
* @param int $width The width to wrap to. Defaults to 72.
|
* @param int $width The width to wrap to. Defaults to 72.
|
||||||
|
@ -355,6 +355,23 @@ class String {
|
||||||
* @return string Formatted text.
|
* @return string Formatted text.
|
||||||
*/
|
*/
|
||||||
public static function wordWrap($text, $width = 72, $break = "\n", $cut = false) {
|
public static function wordWrap($text, $width = 72, $break = "\n", $cut = false) {
|
||||||
|
$paragraphs = explode($break, $text);
|
||||||
|
foreach ($paragraphs as &$paragraph) {
|
||||||
|
$paragraph = String::_wordWrap($paragraph, $width, $break, $cut);
|
||||||
|
}
|
||||||
|
return implode($break, $paragraphs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unicode aware version of wordwrap as helper method.
|
||||||
|
*
|
||||||
|
* @param string $text The text to format.
|
||||||
|
* @param int $width The width to wrap to. Defaults to 72.
|
||||||
|
* @param string $break The line is broken using the optional break parameter. Defaults to '\n'.
|
||||||
|
* @param bool $cut If the cut is set to true, the string is always wrapped at the specified width.
|
||||||
|
* @return string Formatted text.
|
||||||
|
*/
|
||||||
|
protected static function _wordWrap($text, $width = 72, $break = "\n", $cut = false) {
|
||||||
if ($cut) {
|
if ($cut) {
|
||||||
$parts = array();
|
$parts = array();
|
||||||
while (mb_strlen($text) > 0) {
|
while (mb_strlen($text) > 0) {
|
||||||
|
|
Loading…
Reference in a new issue