本文整理汇总了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 ? "…" : '');
}