Skip to content

Commit 99d004a

Browse files
[10.x] Add wordWrap to Str (#48012)
* add wordWrap method * use "\n" instead of EOL constant * formatting * styleci * add tests * Update Str.php * Update Stringable.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 58a8f80 commit 99d004a

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,20 @@ public static function wordCount($string, $characters = null)
13451345
return str_word_count($string, 0, $characters);
13461346
}
13471347

1348+
/**
1349+
* Wrap a string to a given number of characters.
1350+
*
1351+
* @param string $string
1352+
* @param int $characters
1353+
* @param string $break
1354+
* @param bool $cutLongWords
1355+
* @return string
1356+
*/
1357+
public static function wordWrap($string, $characters = 75, $break = "\n", $cutLongWords = false)
1358+
{
1359+
return wordwrap($string, $characters, $break, $cutLongWords);
1360+
}
1361+
13481362
/**
13491363
* Generate a UUID (version 4).
13501364
*

src/Illuminate/Support/Stringable.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,19 @@ public function wordCount($characters = null)
11131113
return Str::wordCount($this->value, $characters);
11141114
}
11151115

1116+
/**
1117+
* Wrap a string to a given number of characters.
1118+
*
1119+
* @param int $characters
1120+
* @param string $break
1121+
* @param bool $cutLongWords
1122+
* @return static
1123+
*/
1124+
public function wordWrap($characters = 75, $break = "\n", $cutLongWords = false)
1125+
{
1126+
return new static(Str::wordWrap($this->value, $characters, $break, $cutLongWords));
1127+
}
1128+
11161129
/**
11171130
* Wrap the string with the given strings.
11181131
*

tests/Support/SupportStrTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,14 @@ public function testWordCount()
901901
$this->assertEquals(3, Str::wordCount('МАМА МЫЛА РАМУ', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'));
902902
}
903903

904+
public function testWordWrap()
905+
{
906+
$this->assertEquals('Hello<br />World', Str::wordWrap('Hello World', 3, '<br />'));
907+
$this->assertEquals('Hel<br />lo<br />Wor<br />ld', Str::wordWrap('Hello World', 3, '<br />', true));
908+
909+
$this->assertEquals('❤Multi<br />Byte☆❤☆❤☆❤', Str::wordWrap('❤Multi Byte☆❤☆❤☆❤', 3, '<br />'));
910+
}
911+
904912
public static function validUuidList()
905913
{
906914
return [

0 commit comments

Comments
 (0)