Splitting a text on word boundaries

Substring extraction of a long piece of text is a common element in web design, mostly in blogs and other CMS for displaying excerpts. The most common way to show an excerpt is to get n number of characters of the text, or n number of words. We will explore both ways. In PHP we frequently use the substr function. However, substr or any of its variants does not split the text at word boundaries, keeping broken words hanging at the end. There are many ways we can prevent this, using some text adjustments. A simple one is shown here.
(more…)

Read More