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 article or a short sentence or two; but for this widget I can’t display more than, say, 200 characters. I could use substr() to chop off the text at 200 chars, but the result would be cutting off in the middle of words– what I really want is to chop the text at the end of the last word before 200 chars.
Share
By using the wordwrap function. It splits the texts in multiple lines such that the maximum width is the one you specified, breaking at word boundaries. After splitting, you simply take the first line:
One thing this one-liner doesn’t handle is the case when the text itself is shorter than the desired width. To handle this edge-case, one should do something like:
The above solution has the problem of prematurely cutting the text if it contains a newline before the actual cutpoint. Here’s a version which solves this problem:
Also, here is the PHPUnit test class used to test the implementation:
Special UTF8 characters like ‘à’ are not handled. Add ‘u’ at the end of the regex to handle it:
$parts = preg_split('/([\s\n\r]+)/u', $string, null, PREG_SPLIT_DELIM_CAPTURE);