php - 如何在 HTML 片段中注入(inject)字符串

标签 php html insert truncate

我想在 HTML 片段的某个位置插入一个阅读更多 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;
}

关于php - 如何在 HTML 片段中注入(inject)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3580095/

相关文章:

php - 如何强制 wordpress 中的特定页面为非 SSL (http)

mysql - SQL REPLACE INTO 不工作

PHP:验证表单并显示警报

php - 如何从sql请求接收多个结果

javascript - 使用 cookies 在 jQuery 中关闭后不显示弹出窗口

javascript - 如何排除 div 触发此事件?

javascript - Internet Explorer TextRange 中的字符偏移量

mysql - 表插入 - 多个 MySQL 数据库

c++ - 如何将结构作为键插入 map ?

PHP 邮件功能安全