cut text after (x) amount of characters

后端 未结 5 1575
谎友^
谎友^ 2021-01-13 07:03

This is in wordpress (not sure that makes a difference)

This bit of php outputs the post title


         


        
5条回答
  •  感情败类
    2021-01-13 07:32

    Another way to cut the string off at the end of a word is with a regex. This one is set to cut off at 100 characters or the nearest word break after 100 characters:

    function firstXChars($string, $chars = 100)
    {
        preg_match('/^.{0,' . $chars. '}(?:.*?)\b/iu', $string, $matches);
        return $matches[0];
    }
    

提交回复
热议问题