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
ba4fd3fa
Commit
ba4fd3fa
authored
Oct 10, 2011
by
Eric Barnes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added str::limit and str::limit_words. Unfortunately php doesn't include mb_str_word_count.
parent
b7b5451a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
0 deletions
+49
-0
str.php
laravel/str.php
+49
-0
No files found.
laravel/str.php
View file @
ba4fd3fa
...
...
@@ -98,6 +98,55 @@ class Str {
return
strlen
(
$value
);
}
/**
* Limit the number of chars in a string
*
* <code>
* // Limit the characters
* echo Str::limit_chars('taylor otwell', 3);
* results in 'tay...'
* </code>
*
* @param string $value
* @param int $length
* @param string $end
* @return string
*/
public
static
function
limit
(
$value
,
$length
=
100
,
$end
=
'...'
)
{
if
(
static
::
length
(
$value
)
<=
$length
)
return
$value
;
if
(
function_exists
(
'mb_substr'
))
{
return
mb_substr
(
$value
,
0
,
$length
)
.
$end
;
}
return
substr
(
$value
,
0
,
$length
)
.
$end
;
}
/**
* Limit the number of words in a string
*
* <code>
* // Limit the words
* echo Str::limit_chars('This is a sentence.', 3);
* results in 'This is a...'
* </code>
*
* @param string $value
* @param int $length
* @param string $end
* @return string
*/
public
static
function
limit_words
(
$value
,
$length
=
100
,
$end
=
'...'
)
{
$count
=
str_word_count
(
$value
,
1
);
if
(
$count
<=
$length
)
return
$value
;
return
implode
(
' '
,
array_slice
(
$count
,
0
,
$length
))
.
$end
;
}
/**
* Convert a string to 7-bit ASCII.
*
...
...
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