Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
562417c5
Commit
562417c5
authored
Nov 24, 2014
by
Alex-Code
Committed by
Qiang Xue
Nov 25, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
truncate and truncateWords can now preserve HTML
new private method `truncateHtml` now used by `truncate` and `truncateWords`
parent
786e4c64
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
2 deletions
+61
-2
BaseStringHelper.php
framework/helpers/BaseStringHelper.php
+61
-2
No files found.
framework/helpers/BaseStringHelper.php
View file @
562417c5
...
...
@@ -98,10 +98,15 @@ class BaseStringHelper
* @param integer $length How many characters from original string to include into truncated string.
* @param string $suffix String to append to the end of truncated string.
* @param string $encoding The charset to use, defaults to charset currently used by application.
* @param boolean $asHtml If the string contains HTML set this to true to preserve it.
* @return string the truncated string.
*/
public
static
function
truncate
(
$string
,
$length
,
$suffix
=
'...'
,
$encoding
=
null
)
public
static
function
truncate
(
$string
,
$length
,
$suffix
=
'...'
,
$encoding
=
null
,
$asHtml
=
false
)
{
if
(
$asHtml
)
{
return
self
::
truncateHtml
(
$string
,
$length
,
$suffix
,
$encoding
?:
Yii
::
$app
->
charset
);
}
if
(
mb_strlen
(
$string
,
$encoding
?:
Yii
::
$app
->
charset
)
>
$length
)
{
return
trim
(
mb_substr
(
$string
,
0
,
$length
,
$encoding
?:
Yii
::
$app
->
charset
))
.
$suffix
;
}
else
{
...
...
@@ -115,10 +120,15 @@ class BaseStringHelper
* @param string $string The string to truncate.
* @param integer $count How many words from original string to include into truncated string.
* @param string $suffix String to append to the end of truncated string.
* @param boolean $asHtml If the string contains HTML set this to true to preserve it.
* @return string the truncated string.
*/
public
static
function
truncateWords
(
$string
,
$count
,
$suffix
=
'...'
)
public
static
function
truncateWords
(
$string
,
$count
,
$suffix
=
'...'
,
$asHtml
=
false
)
{
if
(
$asHtml
)
{
return
self
::
truncateHtml
(
$string
,
$count
,
$suffix
);
}
$words
=
preg_split
(
'/(\s+)/u'
,
trim
(
$string
),
null
,
PREG_SPLIT_DELIM_CAPTURE
);
if
(
count
(
$words
)
/
2
>
$count
)
{
return
implode
(
''
,
array_slice
(
$words
,
0
,
(
$count
*
2
)
-
1
))
.
$suffix
;
...
...
@@ -126,6 +136,55 @@ class BaseStringHelper
return
$string
;
}
}
/**
* Truncate a string while preserving the HTML.
*
* @param string $string The string to truncate
* @param integer $count
* @param string $suffix String to append to the end of the truncated string.
* @param string $encoding
* @return string
*/
private
static
function
truncateHtml
(
$string
,
$count
,
$suffix
,
$encoding
=
false
)
{
$config
=
\HTMLPurifier_Config
::
create
(
null
);
$lexer
=
\HTMLPurifier_Lexer
::
create
(
$config
);
$tokens
=
$lexer
->
tokenizeHTML
(
$string
,
$config
,
null
);
$openTokens
=
0
;
$totalCount
=
0
;
$truncated
=
[];
foreach
(
$tokens
as
$token
)
{
if
(
$token
instanceof
\HTMLPurifier_Token_Start
)
{
//Tag begins
$openTokens
++
;
$truncated
[]
=
$token
;
}
else
if
(
$token
instanceof
\HTMLPurifier_Token_Text
&&
$totalCount
<=
$count
)
{
//Text
if
(
false
===
$encoding
)
{
$token
->
data
=
self
::
truncateWords
(
$token
->
data
,
$count
-
$totalCount
,
''
);
$currentCount
=
str_word_count
(
$token
->
data
);
}
else
{
$token
->
data
=
self
::
truncate
(
$token
->
data
,
$count
-
$totalCount
,
''
,
$encoding
)
.
' '
;
$currentCount
=
mb_strlen
(
$token
->
data
,
$encoding
);
}
$totalCount
+=
$currentCount
;
if
(
1
===
$currentCount
)
{
$token
->
data
=
' '
.
$token
->
data
;
}
$truncated
[]
=
$token
;
}
else
if
(
$token
instanceof
\HTMLPurifier_Token_End
)
{
//Tag ends
$openTokens
--
;
$truncated
[]
=
$token
;
}
else
if
(
$token
instanceof
\HTMLPurifier_Token_Empty
)
{
//Self contained tags, i.e. <img/> etc.
$truncated
[]
=
$token
;
}
if
(
0
===
$openTokens
&&
$totalCount
>=
$count
)
{
break
;
}
}
$context
=
new
\HTMLPurifier_Context
();
$generator
=
new
\HTMLPurifier_Generator
(
$config
,
$context
);
return
$generator
->
generateFromTokens
(
$truncated
)
.
$suffix
;
}
/**
* Check if given string starts with specified substring.
...
...
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