本文整理汇总了PHP中HTMLPurifier_Generator::generateFromTokens方法的典型用法代码示例。如果您正苦于以下问题:PHP HTMLPurifier_Generator::generateFromTokens方法的具体用法?PHP HTMLPurifier_Generator::generateFromTokens怎么用?PHP HTMLPurifier_Generator::generateFromTokens使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTMLPurifier_Generator
的用法示例。
在下文中一共展示了HTMLPurifier_Generator::generateFromTokens方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: truncateHtml
protected static function truncateHtml($string, $count, $suffix, $wordsPerLine, $encoding)
{
$config = \HTMLPurifier_Config::create(null);
$config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
$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;
} elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) {
//Text
if (false === $encoding) {
$token->data = self::truncateWords($token->data, ($count - $totalCount) * $wordsPerLine, '');
$currentWords = str_word_count($token->data);
} else {
$token->data = self::truncate($token->data, ($count - $totalCount) * $wordsPerLine, '', $encoding) . ' ';
$currentWords = mb_strlen($token->data, $encoding);
}
//$totalCount += $currentWords;
if (!$token->is_whitespace) {
$totalCount += intval(ceil($currentWords / $wordsPerLine));
}
//turn into lines
if (1 === $currentWords) {
$token->data = ' ' . $token->data;
}
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_End) {
//Tag ends
$openTokens--;
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_Empty) {
//Self contained tags, i.e. <img/> etc.
if ($token->name == 'img') {
//filter img tag
} else {
$truncated[] = $token;
}
}
if (0 === $openTokens && $totalCount >= $count) {
break;
}
}
$context = new \HTMLPurifier_Context();
$generator = new \HTMLPurifier_Generator($config, $context);
return $generator->generateFromTokens($truncated) . $suffix;
}
示例2: assertGeneration
protected function assertGeneration($tokens, $expect)
{
$generator = new HTMLPurifier_Generator($this->config, $this->context);
$result = $generator->generateFromTokens($tokens);
$this->assertIdentical($expect, $result);
}
示例3: truncateHtml
/**
* 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|boolean $encoding
* @return string
* @since 2.0.1
*/
protected 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;
}
示例4: generate
/**
* Generate textual HTML from tokens
*/
protected function generate($tokens)
{
$generator = new HTMLPurifier_Generator($this->config, $this->context);
return $generator->generateFromTokens($tokens);
}
示例5: truncateHtml
/**
* Truncate a string while preserving the HTML.
*
* @param string $string The string to truncate
* @param int $count
* @param string $suffix String to append to the end of the truncated string.
* @param string|bool $encoding
* @return string
* @since 2.0.1
*/
protected static function truncateHtml($string, $count, $suffix, $encoding = false)
{
$config = \HTMLPurifier_Config::create(null);
$config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
$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;
} elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) {
//Text
if (false === $encoding) {
preg_match('/^(\\s*)/um', $token->data, $prefixSpace) ?: ($prefixSpace = ['', '']);
$token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, '');
$currentCount = self::countWords($token->data);
} else {
$token->data = self::truncate($token->data, $count - $totalCount, '', $encoding);
$currentCount = mb_strlen($token->data, $encoding);
}
$totalCount += $currentCount;
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_End) {
//Tag ends
$openTokens--;
$truncated[] = $token;
} elseif ($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) . ($totalCount >= $count ? $suffix : '');
}