Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在HTML片段中插入字符串_Php_Html_Insert_Truncate - Fatal编程技术网

Php 如何在HTML片段中插入字符串

Php 如何在HTML片段中插入字符串,php,html,insert,truncate,Php,Html,Insert,Truncate,我想在HTML片段的某个位置插入一个read more HTML位,比如在第10个单词之后。然后,我将通过JavaScript将文本隐藏在“阅读更多”之后,并仅在单击时显示 JavaScript部分还可以。至于PHP部分 一开始看起来很简单,但问题是标签弄乱了字数和截断。因此,我需要关闭任何打开的标记,而关闭的对应项超出了“阅读更多”的范围 如果您有任何见解,我们将不胜感激。我不知道这个源代码的确切来源,但我一直在使用这段代码,它非常棒 /** * @desc Cut given plain/H

我想在HTML片段的某个位置插入一个read more HTML位,比如在第10个单词之后。然后,我将通过JavaScript将文本隐藏在“阅读更多”之后,并仅在单击时显示

JavaScript部分还可以。至于PHP部分

一开始看起来很简单,但问题是标签弄乱了字数和截断。因此,我需要关闭任何打开的标记,而关闭的对应项超出了“阅读更多”的范围


如果您有任何见解,我们将不胜感激。

我不知道这个源代码的确切来源,但我一直在使用这段代码,它非常棒

/**
* @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;
}

html中有什么类型的标记?你能给我们看一些代码吗,这样我们就知道你已经有了什么了?我在发表我的问题后不久意识到,为了逐步增强,我实际上应该在html中输出全部内容,并在javascript中执行截断部分。建议的答案有效,所以我接受。这么快的社区也是如此!