Browse Source

Fixes #6207: Added support for truncating HTML strings using `StringHelper::truncate()` and `StringHelper::truncateWords()`

tags/2.0.1
Qiang Xue 10 years ago
parent
commit
87c002d5b3
  1. 1
      framework/CHANGELOG.md
  2. 8
      framework/helpers/BaseStringHelper.php

1
framework/CHANGELOG.md

@ -58,6 +58,7 @@ Yii Framework 2 Change Log
- Enh #5954: `yii message` command now shows user friendly error if it's not able to parse source file (samdark)
- Enh #5983: Added `Inflector::sentence()` (pana1990, qiangxue)
- Enh #6113: Improved debugger configuration and request UI (schmunk42)
- Enh #6207: Added support for truncating HTML strings using `StringHelper::truncate()` and `StringHelper::truncateWords()` (Alex-Code)
- Enh: `Console::confirm()` now uses `Console::stdout()` instead of `echo` to be consistent with all other functions (cebe)
- Enh: `yii\rbac\DbManager` migration now uses database component specified in component settings instead of always using default `db` (samdark)
- Enh: Added `yii\base\Controller::renderContent()` (qiangxue)

8
framework/helpers/BaseStringHelper.php

@ -98,7 +98,7 @@ 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.
* @param boolean $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags
* @return string the truncated string.
*/
public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
@ -120,7 +120,7 @@ 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.
* @param boolean $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags
* @return string the truncated string.
*/
public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)
@ -143,10 +143,10 @@ class BaseStringHelper
* @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
* @param string|boolean $encoding
* @return string
*/
private static function truncateHtml($string, $count, $suffix, $encoding = false)
protected static function truncateHtml($string, $count, $suffix, $encoding = false)
{
$config = \HTMLPurifier_Config::create(null);
$lexer = \HTMLPurifier_Lexer::create($config);

Loading…
Cancel
Save