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

前端 未结 27 1462
猫巷女王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条回答
  •  Happy的楠姐
    2020-11-22 07:55

    /*
    Cut the string without breaking any words, UTF-8 aware 
    * param string $str The text string to split
    * param integer $start The start position, defaults to 0
    * param integer $words The number of words to extract, defaults to 15
    */
    function wordCutString($str, $start = 0, $words = 15 ) {
        $arr = preg_split("/[\s]+/",  $str, $words+1);
        $arr = array_slice($arr, $start, $words);
        return join(' ', $arr);
    }
    

    Usage:

    $input = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna liqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';
    echo wordCutString($input, 0, 10); 
    

    This will output first 10 words.

    The preg_split function is used to split a string into substrings. The boundaries along which the string is to be split, are specified using a regular expressions pattern.

    preg_split function takes 4 parameters, but only the first 3 are relevant to us right now.

    First Parameter – Pattern The first parameter is the regular expressions pattern along which the string is to be split. In our case, we want to split the string across word boundaries. Therefore we use a predefined character class \s which matches white space characters such as space, tab, carriage return and line feed.

    Second Parameter – Input String The second parameter is the long text string which we want to split.

    Third Parameter – Limit The third parameter specifies the number of substrings which should be returned. If you set the limit to n, preg_split will return an array of n elements. The first n-1 elements will contain the substrings. The last (n th) element will contain the rest of the string.

提交回复
热议问题