How to Truncate a string in PHP to the word closest to a certain number of characters?

前端 未结 27 1122
猫巷女王i
猫巷女王i 2020-11-22 07:28

I have a code snippet written in PHP that pulls a block of text from a database and sends it out to a widget on a webpage. The original block of text can be a lengthy artic

相关标签:
27条回答
  • 2020-11-22 08:04

    I used this before

    <?php
        $your_desired_width = 200;
        $string = $var->content;
        if (strlen($string) > $your_desired_width) {
            $string = wordwrap($string, $your_desired_width);
            $string = substr($string, 0, strpos($string, "\n")) . " More...";
        }
        echo $string;
    ?>
    
    0 讨论(0)
  • 2020-11-22 08:06

    Keep in mind whenever you're splitting by "word" anywhere that some languages such as Chinese and Japanese do not use a space character to split words. Also, a malicious user could simply enter text without any spaces, or using some Unicode look-alike to the standard space character, in which case any solution you use may end up displaying the entire text anyway. A way around this may be to check the string length after splitting it on spaces as normal, then, if the string is still above an abnormal limit - maybe 225 characters in this case - going ahead and splitting it dumbly at that limit.

    One more caveat with things like this when it comes to non-ASCII characters; strings containing them may be interpreted by PHP's standard strlen() as being longer than they really are, because a single character may take two or more bytes instead of just one. If you just use the strlen()/substr() functions to split strings, you may split a string in the middle of a character! When in doubt, mb_strlen()/mb_substr() are a little more foolproof.

    0 讨论(0)
  • 2020-11-22 08:06

    I know this is old, but...

    function _truncate($str, $limit) {
        if(strlen($str) < $limit)
            return $str;
        $uid = uniqid();
        return array_shift(explode($uid, wordwrap($str, $limit, $uid)));
    }
    
    0 讨论(0)
  • 2020-11-22 08:07

    The following solution was born when I've noticed a $break parameter of wordwrap function:

    string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )

    Here is the solution:

    /**
     * Truncates the given string at the specified length.
     *
     * @param string $str The input string.
     * @param int $width The number of chars at which the string will be truncated.
     * @return string
     */
    function truncate($str, $width) {
        return strtok(wordwrap($str, $width, "...\n"), "\n");
    }
    

    Example #1.

    print truncate("This is very long string with many chars.", 25);
    

    The above example will output:

    This is very long string...
    

    Example #2.

    print truncate("This is short string.", 25);
    

    The above example will output:

    This is short string.
    
    0 讨论(0)
  • 2020-11-22 08:08

    May be this will help someone:

    <?php
    
        $string = "Your line of text";
        $spl = preg_match("/([, \.\d\-''\"\"_()]*\w+[, \.\d\-''\"\"_()]*){50}/", $string, $matches);
        if (isset($matches[0])) {
            $matches[0] .= "...";
            echo "<br />" . $matches[0];
        } else {
            echo "<br />" . $string;
        }
    
    ?>
    
    0 讨论(0)
  • 2020-11-22 08:10

    Here you can try this

    substr( $str, 0, strpos($str, ' ', 200) ); 
    
    0 讨论(0)
提交回复
热议问题