s950329
12/22/2017 - 9:48 AM

替換文字為超連結

替換太長的文字為查看更多超連結

/**
 * 替換太長的文字為查看更多超連結
 *
 * @param        $text
 * @param        $length
 * @param        $link
 * @param string $linkWord
 *
 * @return string
 */
function limitTextLength($text, $length, $link, $linkWord = '查看更多')
{
    // strip tags to avoid breaking any html
    $text = strip_tags($text);

    if (strlen($text) > $length) {
        // truncate string
        $stringCut = substr($text, 0, $length);

        // make sure it ends in a word so assassinate doesn't become ass...
        $text = substr($stringCut, 0, strrpos($stringCut, ' '))
            . '... <a href="' . $link . '">' . $linkWord . '</a>';
    }

    return $text;
}