Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yeray Santana Hualde
syncEnrollments
Commits
17ea0936
Commit
17ea0936
authored
Oct 15, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed str test cases and str::limit function.
parent
a6eaa069
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
10 deletions
+11
-10
routes.php
application/routes.php
+1
-0
str.php
laravel/str.php
+8
-8
StrTest.php
tests/Cases/StrTest.php
+2
-2
No files found.
application/routes.php
View file @
17ea0936
...
...
@@ -39,6 +39,7 @@ return array(
'GET /'
=>
function
()
{
return
Laravel\Str
::
limit
(
'This is a string of text'
,
3
,
'...'
);
return
View
::
make
(
'home.index'
);
},
...
...
laravel/str.php
View file @
17ea0936
...
...
@@ -101,11 +101,8 @@ class Str {
/**
* Limit the number of characters in a string.
*
* Word integrity is preserved, so the number of characters in the
* truncated string will be rounded to the nearest word ending.
*
* <code>
* // Returns "Tay
lor
..."
* // Returns "Tay..."
* echo Str::limit('Taylor Otwell', 3);
*
* // Limit the number of characters and append a custom ending
...
...
@@ -113,17 +110,20 @@ class Str {
* </code>
*
* @param string $value
* @param int $l
ength
* @param int $l
imit
* @param string $end
* @return string
*/
public
static
function
limit
(
$value
,
$limit
=
100
,
$end
=
'...'
)
{
if
(
static
::
length
(
$value
)
<
$limit
)
return
$value
;
if
(
static
::
length
(
$value
)
<
=
$limit
)
return
$value
;
$limit
=
preg_replace
(
'/\s+?(\S+)?$/'
,
''
,
substr
(
$value
,
0
,
$limit
));
if
(
function_exists
(
'mb_substr'
))
{
return
mb_substr
(
$value
,
0
,
$limit
,
Config
::
get
(
'application.encoding'
))
.
$end
;
}
return
(
static
::
length
(
$limit
)
==
static
::
length
(
$value
))
?
$value
:
$limit
.
$end
;
return
substr
(
$value
,
0
,
$limit
)
.
$end
;
}
/**
...
...
tests/Cases/StrTest.php
View file @
17ea0936
...
...
@@ -45,7 +45,7 @@ class StrTest extends PHPUnit_Framework_TestCase {
public
function
test_limit_words
()
{
$this
->
assertEquals
(
'This is a...'
,
Laravel\Str
::
limit_
words
(
'This is a string of text'
,
3
,
'...'
));
$this
->
assertEquals
(
'This is a string '
,
Laravel\Str
::
limit_
words
(
'This is a string of text'
,
4
,
' '
));
$this
->
assertEquals
(
'This is a...'
,
Laravel\Str
::
words
(
'This is a string of text'
,
3
,
'...'
));
$this
->
assertEquals
(
'This is a string '
,
Laravel\Str
::
words
(
'This is a string of text'
,
4
,
' '
));
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment