本文整理汇总了PHP中Twig_Environment::getCharset方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::getCharset方法的具体用法?PHP Twig_Environment::getCharset怎么用?PHP Twig_Environment::getCharset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::getCharset方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1:
function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $separator = '...')
{
if (mb_strlen($value, $env->getCharset()) > $length) {
return mb_substr($value, 0, $length, $env->getCharset()) . $separator;
}
return $value;
}
示例2: array
function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
{
$sentences = array();
$previous = mb_regex_encoding();
mb_regex_encoding($env->getCharset());
$pieces = mb_split($separator, $value);
mb_regex_encoding($previous);
foreach ($pieces as $piece) {
while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
$sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
$piece = mb_substr($piece, $length, 2048, $env->getCharset());
}
$sentences[] = $piece;
}
return implode($separator, $sentences);
}
示例3: dumpData
public function dumpData(\Twig_Environment $env, Data $data, $maxDepth = 0)
{
$this->dumper->setCharset($env->getCharset());
$this->dumper->dump($data, null, array('maxDepth' => $maxDepth));
$dump = stream_get_contents($this->output, -1, 0);
rewind($this->output);
ftruncate($this->output, 0);
return str_replace("\n</pre", '</pre', rtrim($dump));
}
示例4: dump
public function dump(\Twig_Environment $env, $context)
{
if (!$env->isDebug()) {
return;
}
if (2 === func_num_args()) {
$vars = array();
foreach ($context as $key => $value) {
if (!$value instanceof \Twig_Template) {
$vars[$key] = $value;
}
}
$vars = array($vars);
} else {
$vars = func_get_args();
unset($vars[0], $vars[1]);
}
$dump = fopen('php://memory', 'r+b');
$this->dumper->setCharset($env->getCharset());
foreach ($vars as $value) {
$this->dumper->dump($this->cloner->cloneVar($value), $dump);
}
return stream_get_contents($dump, -1, 0);
}
示例5: ucfirst
function twig_capitalize_string_filter(Twig_Environment $env, $string)
{
if (null !== ($charset = $env->getCharset())) {
return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset) . mb_strtolower(mb_substr($string, 1, mb_strlen($string, $charset), $charset), $charset);
}
return ucfirst(strtolower($string));
}
示例6: truncateText
public function truncateText(\Twig_Environment $env, $value, $length = 64, $preserve = false, $separator = '...')
{
try {
$value = (string) $value;
} catch (\Exception $e) {
$value = '';
}
if (function_exists('mb_get_info')) {
if (mb_strlen($value, $env->getCharset()) > $length) {
if ($preserve) {
// If breakpoint is on the last word, return the value without separator.
if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) {
return $value;
}
$length = $breakpoint;
}
return rtrim(mb_substr($value, 0, $length, $env->getCharset())) . $separator;
}
return $value;
}
if (strlen($value) > $length) {
if ($preserve) {
if (false !== ($breakpoint = strpos($value, ' ', $length))) {
$length = $breakpoint;
}
}
return rtrim(substr($value, 0, $length)) . $separator;
}
return $value;
}
示例7: trancate
/**
* 文字列を一定の長さで切り取る
* 切り取った後は、指定のマーカーを付与する
*
* @access public
* @param Twig_Environment
* @param String
* @param Int 切り出す開始位置
* @param Int 切り出す終了位置
* @param String 切り出した文字列の後に付加する文字
* @return String
*/
public static function trancate(Twig_Environment $env, $string, $start = 0, $end = 30, $marker = "[...]")
{
return mb_strimwidth($string, $start, $end, $marker, $env->getCharset());
}
示例8: twig_capitalize_string_filter
/**
* Returns a capitalized string.
*
* @param Twig_Environment $env A Twig_Environment instance
* @param string $string A string
*
* @return string The capitalized string
*/
function twig_capitalize_string_filter(Twig_Environment $env, $string)
{
$charset = $env->getCharset();
return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset) . mb_strtolower(mb_substr($string, 1, 2147483647, $charset), $charset);
}
示例9: truncate
/**
* Cuts a string to the length of $length and replaces the last characters
* with the ellipsis if the text is longer than length.
*
* @param \Twig_Environment $env
* @param string $text String to truncate.
* @param int $length Length of returned string, including ellipsis.
* @param string $ellipsis Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
* @param bool $exact If false, $text will not be cut mid-word
* @param bool $html If true, HTML tags would be handled correctly
*
* @return string
*/
public static function truncate(\Twig_Environment $env, $text, $length = 100, $ellipsis = '...', $exact = true, $html = false)
{
if ($html && $ellipsis == '...' && $env->getCharset() == 'UTF-8') {
$ellipsis = "…";
}
if (!function_exists('mb_strlen')) {
class_exists('Multibyte');
}
$openTags = array();
if ($html) {
$text = html_entity_decode($text, null, $env->getCharset());
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
return $text;
}
$totalLength = mb_strlen(strip_tags($ellipsis));
$truncate = '';
preg_match_all('/(<\\/?([\\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
foreach ($tags as $tag) {
if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
if (preg_match('/<[\\w]+[^>]*>/s', $tag[0])) {
array_unshift($openTags, $tag[2]);
} elseif (preg_match('/<\\/([\\w]+)[^>]*>/s', $tag[0], $closeTag)) {
$pos = array_search($closeTag[1], $openTags);
if ($pos !== false) {
array_splice($openTags, $pos, 1);
}
}
}
$truncate .= $tag[1];
$contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
if ($contentLength + $totalLength > $length) {
$left = $length - $totalLength;
$entitiesLength = 0;
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
foreach ($entities[0] as $entity) {
if ($entity[1] + 1 - $entitiesLength <= $left) {
$left--;
$entitiesLength += mb_strlen($entity[0]);
} else {
break;
}
}
}
$truncate .= mb_substr($tag[3], 0, $left + $entitiesLength);
break;
} else {
$truncate .= $tag[3];
$totalLength += $contentLength;
}
if ($totalLength >= $length) {
break;
}
}
} else {
if (mb_strlen($text) <= $length) {
return $text;
}
$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
}
if (!$exact) {
$spacepos = mb_strrpos($truncate, ' ');
if ($html) {
$truncateCheck = mb_substr($truncate, 0, $spacepos);
$lastOpenTag = mb_strrpos($truncateCheck, '<');
$lastCloseTag = mb_strrpos($truncateCheck, '>');
if ($lastOpenTag > $lastCloseTag) {
preg_match_all('/<[\\w]+[^>]*>/s', $truncate, $lastTagMatches);
$lastTag = array_pop($lastTagMatches[0]);
$spacepos = mb_strrpos($truncate, $lastTag) + mb_strlen($lastTag);
}
$bits = mb_substr($truncate, $spacepos);
preg_match_all('/<\\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
if (!empty($droppedTags)) {
if (!empty($openTags)) {
foreach ($droppedTags as $closingTag) {
if (!in_array($closingTag[1], $openTags)) {
array_unshift($openTags, $closingTag[1]);
}
}
} else {
foreach ($droppedTags as $closingTag) {
$openTags[] = $closingTag[1];
}
}
}
}
$truncate = mb_substr($truncate, 0, $spacepos);
//.........这里部分代码省略.........
示例10: apply_function_twig_filter
/**
* @param Twig_Environment $env
* @param $context
* @return string
*/
public function apply_function_twig_filter(Twig_Environment $env, $context, $string)
{
// get the current charset for instance
$charset = $env->getCharset();
$args = func_get_args();
$func = current(array_splice($args, 2, 1));
return call_user_func_array($func, $args);
}
示例11: safeTruncate
/**
* Filter used to safely truncate a string with html
*
* @param \Twig_Environment $env
* @param string $value
* @param int $length
* @param bool $preserve
* @param string $separator
*
* @return string
*/
public function safeTruncate(\Twig_Environment $env, $value, $length = 30, $preserve = true, $separator = '...')
{
$charset = $env->getCharset();
if ($this->isMultiByteStringAvailable() && $this->getMultiByteString()) {
$strlen = function ($string, $encoding = null) {
return mb_strlen($string, $encoding);
};
$substr = function ($string, $start, $length = null, $encoding = null) {
return mb_substr($string, $start, $length, $encoding);
};
$strpos = function ($haystack, $needle, $offset = null, $encoding = null) {
return mb_strpos($haystack, $needle, $offset, $encoding);
};
} else {
$strlen = function ($string, $encoding = null) {
return strlen($string);
};
$substr = function ($string, $start, $length = null, $encoding = null) {
return substr($string, $start, $length);
};
$strpos = function ($haystack, $needle, $offset = null, $encoding = null) {
return strpos($haystack, $needle, $offset);
};
}
// First, strip tags to get a exact chars count
$strippedValue = strip_tags($value);
// Initialize the breakpoint to the exact length for now
$breakpoint = $length;
// Check if the string is bigger than the available length, otherwise, there is nothing to do
if (strlen($strippedValue) > $length) {
// Initialize the pipedValue used to replace spaces by pipe in html tags
$pipedValue = $value;
// Check if there is html tags in the original value
if ($strippedValue !== $value) {
// Replace spaces in html tags by pipes to easily split the string by spaces available in the text
$pipedValue = preg_replace_callback('#<([^>]*)( )([^<]*)>#', function ($matches) {
return str_replace(' ', '|', $matches[0]);
}, $value);
}
// Initialize the available words
$words = explode(' ', $substr($strippedValue, 0, $breakpoint, $charset));
$availableWords = count($words);
$lastWord = '';
// If we have to preserve words
if ($preserve) {
// First check if there is any spaces available in the string
if (false !== $strpos($substr($strippedValue, 0, $length, $charset), ' ', null, $charset)) {
// Get a breakpoint at the next space after the available length
if (false !== ($nextSpace = $strpos($substr($strippedValue, $length, $strlen($strippedValue, $charset), $charset), ' ', null, $charset))) {
// Update breakpoint to next space
$breakpoint += $nextSpace;
// Split the string by spaces until the breakpoint
$words = explode(' ', $substr($strippedValue, 0, $breakpoint, $charset));
// If the space is not the next char, we should remove last word
if ($nextSpace > 0) {
// Remove the last element which is outside the scope of defined length
array_pop($words);
}
// Get the count of available words
$availableWords = count($words);
} else {
// Otherwise remove the last word from the array
$availableWords--;
}
} else {
// Otherwise remove the last word from the array
$availableWords--;
}
} else {
// Otherwise, preserve the last word part and remove it from the array
$lastWord = $words[count($words) - 1];
$availableWords--;
}
// Split the piped value by spaces
$words = explode(' ', $pipedValue);
// Remove words that are not in the scope defined by the length
$words = array_slice($words, 0, $availableWords);
if ($lastWord !== '') {
$words[] = $lastWord;
}
$pipedValue = implode(' ', $words);
// Replace back pipes in html tags to spaces
$value = preg_replace_callback('#<([^>]*)(|)([^<]*)>#', function ($matches) {
return str_replace('|', ' ', $matches[0]);
}, $pipedValue);
// Finally close all unclosed tags and add trailing separator
return $this->closeTags($value . $separator);
}
return $value;
//.........这里部分代码省略.........