本文整理匯總了PHP中StringUtils::utf8SafeTruncate方法的典型用法代碼示例。如果您正苦於以下問題:PHP StringUtils::utf8SafeTruncate方法的具體用法?PHP StringUtils::utf8SafeTruncate怎麽用?PHP StringUtils::utf8SafeTruncate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類StringUtils
的用法示例。
在下文中一共展示了StringUtils::utf8SafeTruncate方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: truncateToMax
/**
* Returns a string truncated to fit inside a meta.
*
* @param mixed $obj - object that has the schema
* (Node | NodeRef | Element | Aspect)
* @param string $metaName
* @param string $string - string to be truncated
* @param bool $ellipsis - if set, output is appended with three dots
* @return string
*/
public static function truncateToMax($obj, $metaName, $string, $ellipsis = false)
{
switch (true) {
case $obj instanceof Element:
case $obj instanceof Aspect:
break;
case $obj instanceof NodeRef:
case $obj instanceof Node:
$obj = $obj->Element;
break;
default:
throw new Exception(__CLASS__ . '::' . __FUNCTION__ . ": Expected first argument to be instance of Element, Aspect, Node or NodeRef");
}
$metaValidationArray = $obj->Schema->getMetaDef($metaName)->Validation->toArray();
$max = $metaValidationArray['max'];
$maxBytes = max($max, 255);
$truncated = false;
if (strlen($string) > $maxBytes) {
$truncated = true;
$string = StringUtils::utf8SafeTruncate($string, $maxBytes - ($ellipsis ? 3 : 0));
}
if ($max < 255 && StringUtils::charCount($string) > $max) {
$truncated = true;
$string = StringUtils::utf8Substr($string, 0, $max - ($ellipsis ? 1 : 0));
}
return $string . ($truncated && $ellipsis ? "…" : '');
}