PHP字符串截取,支持中英文混排
时间:2010-10-19 来源:phpwriter
文件名:modifier.smart_truncate_gb2312_string.php
程序代码 <?php
function smarty_modifier_smart_truncate_gb2312_string($string, $length, $etc = '...')
{
$result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'GB2312');
$strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
{
if (ord(substr($string, $i, 1)) > 128)
{
if ($length < 1.0)
{
break;
}
$result .= substr($string, $i, 2);
$length -= 1.0;
$i++;
}
else
{
$result .= substr($string, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'GB2312');
if ($i < $strlen)
{
$result .= $etc;
}
return $result;
}
?>
文件名:modifier.smart_truncate_utf8_string.php
程序代码 <?php
function smarty_modifier_smart_truncate_utf8_string($string, $length, $etc = '...')
{
$result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
$strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
{
if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
{
if ($length < 1.0)
{
break;
}
$result .= substr($string, $i, $number);
$length -= 1.0;
$i += $number - 1;
}
else
{
$result .= substr($string, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
if ($i < $strlen)
{
$result .= $etc;
}
return $result;
}
?>
网上看到的,这个可能是谁自己写的一个smarty插件。
程序代码 <?php
function smarty_modifier_smart_truncate_gb2312_string($string, $length, $etc = '...')
{
$result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'GB2312');
$strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
{
if (ord(substr($string, $i, 1)) > 128)
{
if ($length < 1.0)
{
break;
}
$result .= substr($string, $i, 2);
$length -= 1.0;
$i++;
}
else
{
$result .= substr($string, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'GB2312');
if ($i < $strlen)
{
$result .= $etc;
}
return $result;
}
?>
文件名:modifier.smart_truncate_utf8_string.php
程序代码 <?php
function smarty_modifier_smart_truncate_utf8_string($string, $length, $etc = '...')
{
$result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
$strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
{
if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
{
if ($length < 1.0)
{
break;
}
$result .= substr($string, $i, $number);
$length -= 1.0;
$i += $number - 1;
}
else
{
$result .= substr($string, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
if ($i < $strlen)
{
$result .= $etc;
}
return $result;
}
?>
网上看到的,这个可能是谁自己写的一个smarty插件。
相关阅读 更多 +