【问题标题】:Facebook-like "show more" button for a string with URLs带有 URL 的字符串的类似 Facebook 的“显示更多”按钮
【发布时间】:2017-06-17 17:47:54
【问题描述】:

我正在尝试使用类似于 Facebook 的 show more 行为的功能。 我希望它在以下情况下修剪字符串:

  • 长度超过 200 个字符。
  • /n 出现次数超过 5 次。

听起来很简单,而且我已经有了一个初始函数(仅按长度执行,我还没有实现 /n 的出现):

function contentShowMore($string, $max_length) {
    if(mb_strlen($string, 'utf-8') <= $max_length) {
        return $string; // return the original string if haven't passed $max_length
    } else {
        $teaser = mb_substr($string, 0, $max_length); // trim to max length
        $dots = '<span class="show-more-dots"> ...</span>'; // add dots
        $show_more_content = mb_substr($string, $max_length); // get the hidden content
        $show_more_wrapper = '<span class="show-more-content">'.$show_more_content.'</span>'; // wrap it

        return $teaser.$dots.$show_more_wrapper; // connect all together for usage on HTML.
    }
}

问题是字符串可能包含 URL,因此会破坏它们。我需要找到一种方法来制作一个功能性的show-more 按钮,它可以检查长度、换行符并且不会剪切 URL。

谢谢!

示例:

输入:contentShowMore("hello there http://google.com/ good day!", 20)

输出:

hello there http://g
<span class="show-more-dots"> ...</span>
<span class="show-more-content">oogle.com/ good day!</span>

我想要的输出:

hello there http://google.com/
<span class="show-more-dots"> ...</span>
<span class="show-more-content"> good day!</span>

【问题讨论】:

  • 你能分享你的输入和预期输出吗?
  • 没有特定的输入和输出,因为这个函数可以运行很多。但我可以举个例子。我会把它添加到我的问题中,等等
  • 请问您将第二个参数传递为20,以及您是如何获得它的,我会帮助您,为了更好地理解我只想了解您的问题。
  • 是的,但正如您所见,它也削减了 URL。如果它不是 URL,我需要它来剪切字符串(如果是,则在 URL 之后剪切字符串)。谢谢!

标签: php html string substring trim


【解决方案1】:

找到了解决办法!

function contentShowMore($string, $max_length, $max_newlines) {

    $trim_str = trim($string);

    if(mb_strlen($trim_str, 'utf-8') <= $max_length && substr_count($trim_str, "\n") < $max_newlines) { // return the original if short, or less than X newlines
        return $trim_str;
    } else {
        $teaser = mb_substr($trim_str, 0, $max_length); // text to show
        $show_more_content = mb_substr($trim_str, $max_length);

        // the read more might have cut a string (or worse - an URL) in the middle of it.
        // so we will take all the rest of the string before the next whitespace and will add it back to the teaser.
        $content_parts = explode(' ', $show_more_content, 2); // [0] - before first space, [1] - after first space
        $teaser .= $content_parts[0];

        if(isset($content_parts[1])) { // if there are still leftover strings, its on show more! :)
            $show_more_content = $content_parts[1];
        }

        // NOW WERE CHEKING MAX NEWLINES.
        $teaser_parts = explode("\n", $teaser); // break to array.
        $teaser = implode("\n", array_slice($teaser_parts, 0, $max_newlines)); // take the first $max_newlines lines and use them as teaser.
        $show_more_content = implode("\n", array_slice($teaser_parts, $max_newlines)) . ' ' . $show_more_content; // connect the rest to the hidden content.

        if(mb_strlen($show_more_content, "UTF-8") === 0) {
            return $trim_str; // nothing to hide - return original.
        } else {
            $show_more_wrapper = '<span class="show-more-content">'.$show_more_content.'</span>';

            $dots = '<span class="show-more-dots"> ...</span>'; // dots will be visible between the teaser and the hidden.
            $button = ' <span class="show-more">Show more</span>';

            return $teaser.$dots.$button.$show_more_wrapper; // connect ingredients
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多