String “这是怎么回事?”;单击查看更多信息”;功能实现了吗?
我们经常看到网站显示文章的前几行,然后附加。。。[更多]以便感兴趣的人可以点击它查看全文 为了实现这个功能,我们首先需要找出文章文本应该被剪切到什么地方以附加。。。[更多] 因为必须有一些HTML/CSS与文章文本相结合,所以我们必须记住在计算文本长度时忽略它们 但是有一点我就是搞不清楚,如果在文章文本的前几行混合了多种语言,文本的长度将很难计算,因为字符的长度可能是可变的String “这是怎么回事?”;单击查看更多信息”;功能实现了吗?,string,internationalization,String,Internationalization,我们经常看到网站显示文章的前几行,然后附加。。。[更多]以便感兴趣的人可以点击它查看全文 为了实现这个功能,我们首先需要找出文章文本应该被剪切到什么地方以附加。。。[更多] 因为必须有一些HTML/CSS与文章文本相结合,所以我们必须记住在计算文本长度时忽略它们 但是有一点我就是搞不清楚,如果在文章文本的前几行混合了多种语言,文本的长度将很难计算,因为字符的长度可能是可变的 我们应该如何解决这个问题?我不知道这个源代码的确切来源,但我一直在使用这段代码,您可以在它的注释中找到解释: /** *
我们应该如何解决这个问题?我不知道这个源代码的确切来源,但我一直在使用这段代码,您可以在它的注释中找到解释:
/**
* @desc Cut given plain/HTML text nicely
* @param string text to cut
* @param int approximetly length of desired text length
* @param int optional length, how far text can variante from approximetly length
* @param bool optional can we cut words
* @param bool optional do we need to append three dots to the end of cutted text
* @return string cutted text
*/
function htmlSubstr($text, $approxLength, $lengthOffset = 20, $cutWords = FALSE, $dotsAtEnd = TRUE) {
mb_internal_encoding('UTF-8');
// $approxLength:
// The approximate length you want the concatenated text to be
// $lengthOffset:
// The variation in how long the text can be in this example text
// length will be between 200 and 200-20=180 characters and the
// character where the last tag ends
// Reset tag counter & quote checker
$tag_counter = 0;
$quotes_on = FALSE;
// Check if the text is too long
if (mb_strlen($text) > $approxLength) {
// Reset the tag_counter and pass through (part of) the entire text
$c = 0;
for ($i = 0; $i < mb_strlen($text); $i++) {
// Load the current character and the next one
// if the string has not arrived at the last character
$current_char = mb_substr($text,$i,1);
if ($i < mb_strlen($text) - 1) {
$next_char = mb_substr($text,$i + 1,1);
} else {
$next_char = "";
}
// First check if quotes are on
if (!$quotes_on) {
// Check if it's a tag
// On a "<" add 3 if it's an opening tag (like <a href...)
// or add only 1 if it's an ending tag (like </a>)
if ($current_char == '<') {
if ($next_char == '/') {
$tag_counter += 1;
} else {
$tag_counter += 3;
}
}
// Slash signifies an ending (like </a> or ... />)
// substract 2
if ($current_char == '/' && $tag_counter <> 0) $tag_counter -= 2;
// On a ">" substract 1
if ($current_char == '>') $tag_counter -= 1;
// If quotes are encountered, start ignoring the tags
// (for directory slashes)
if ($current_char == '"') $quotes_on = TRUE;
} else {
// IF quotes are encountered again, turn it back off
if ($current_char == '"') $quotes_on = FALSE;
}
// Count only the chars outside html tags
if($tag_counter == 2 || $tag_counter == 0) $c++;
// Check if the counter has reached the minimum length yet,
// then wait for the tag_counter to become 0, and chop the string there
if ($c > $approxLength - $lengthOffset && $tag_counter == 0 && ($next_char == ' ' || $cutWords == TRUE)) {
$text = mb_substr($text,0,$i + 1);
if($dotsAtEnd){
$text .= '...';
}
return $text;
}
}
}
return $text;
}
/**
*@desc剪切给定的纯/HTML文本
*@param要剪切的字符串文本
*@param int近似于所需文本长度的长度
*@param int可选长度,文本可以与近似长度相差多远
*@param bool可选我们可以切字吗
*@param bool可选我们是否需要在剪切文本的末尾附加三个点
*@return字符串剪切文本
*/
函数htmlSubstr($text、$approvxlength、$lengthOffset=20、$cutWords=FALSE、$dotsAtEnd=TRUE){
mb_内部_编码(“UTF-8”);
//$approxLength:
//希望连接文本的大致长度
//$lengthOffset:
//本示例文本中文本长度的变化
//长度将介于200和200-20=180个字符之间,并且
//最后一个标记结束的字符
//重置标签计数器和报价检查器
$tag_计数器=0;
$quotes_on=FALSE;
//检查文本是否过长
如果(mb_strlen($text)>$approvxlength){
//重置tag_计数器并传递(部分)整个文本
$c=0;
对于($i=0;$i')$tag_counter-=1;
//如果遇到引号,开始忽略标记
//(用于目录斜杠)
如果($current_char=='”)$quotes_on=TRUE;
}否则{
//如果再次遇到引号,请将其禁用
如果($current_char=='”)$quotes_on=FALSE;
}
//只计算html标记之外的字符
如果($tag_counter==2 |$$tag_counter==0)$c++;
//检查计数器是否已达到最小长度,
//然后等待tag_计数器变为0,然后在那里切掉字符串
如果($c>$approxLength-$lengthOffset&&$tag_counter==0&&($next_char==''| |$cutWords==TRUE)){
$text=mb_substr($text,0,$i+1);
如果($dotsAtEnd){
$text.='…';
}
返回$text;
}
}
}
返回$text;
}