PHP has several functions which can truncate a string to grab a portion of the string up to certain number of characters. However, there is possibility that a word can be cut off in the middle of nowhere into two, making the separated word unintelligible. The situation is not an error, but by design, as most commands such as substr() simply return portion of string instructed by start position and length parameters.

It may be more comprehensible for visitors to your site to read a complete and full word without been chopped off or broken apart in the middle into two, with the later part absent from the sentences after been cut off due to limited number of characters required.

It’s possible to re-code the PHP script to cut off the text at the end of the nearest last word before or after a certain number of characters in full so that it’s a complete word. Here are some examples of PHP algorithms that can be used to achieve the effect of not cutting off words into two parts while grabbing a portion of a string.

Example 1


$length = 150; //modify for desired width
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length));

The above code will return a string which is cut off right after the last word before the last space before the maximum character limit is hit.

Example 2

$length = 150; //modify for desired width
if (strlen($string) <= $length) {
   $string = $string; //do nothing
} else {
   $string = substr($string, 0, strpos(wordwrap($string, $length), "\n"));
}

PHP code above uses the wordwrap() function to split the texts into multiple lines. wordwrap() by default will split at the boundary right before maximum character limit so that the no word is broken apart. Then, strpos() is used to know the exact length of the first line, allowing substr() to cut accurately without breaking apart a word. Again, the truncate boundary is the end of the last full word right before desired length of return string.

Example 3

$length = 150; //modify for desired width
if (strlen($string) <= $length) {
   $string = $string; //do nothing
} else {
   $string = substr($string,0,strpos($string,' ',$length));
}

Code above will truncate the string at the first space after the desired length, useless for people who wants the string to have at least a minimum amount of characters, but immediately cut off right after the whole word exceeding the limit.

Example 4

$length = 150; //modify for desired width
if (strlen($string) <= $length) {
   $string = $string; //do nothing
} else {
   $string = substr($string, 0, strpos(substr($string, 0, $length), ' '));
}

If code in example 3 does not return desired result, another variant above truncates the string at the nearest word under the maximum string length.

Example 5

$length = 150; //modify for desired width
if (strlen($string) <= $length) {
   $string = $string; //do nothing
} else {
   $matches = array();
   $result = preg_match("/^(.{1,199})[\s]/i", $string, $matches);
   $string = $result[1];
}

Note that if your string contains special characters or foreign languagessuch as Arabic, Chinese, Japanese, Korean, Russian and etc, it may not be possible to use a space character to split words. In addition, together with some non-ASCII characters, these words may take up two bytes of data instead of just one, causing the strlen() function to calculate the length of the string longer than it's (resulting in shorter returned string). If you're encountering this issue, use mb_strlen() function instead.

Some words may be extremely long where a single word may exceed the character limit set. In this case, it's imperative to double check the length of the truncated string to check if it is longer than the limit. If it is, just truncate it at the set limit.

Tip: The sentence have been truncated, so it may be recommended to put a "...." to the end of truncated string so that end user will know it's part of longer article. To append "..." to the end of the string, use the following command:

$string .= '...';

Related Posts

  • Adimin Baik

    Hi,
    thanks for great tips,
    So, I completing your idea to be:

    $length = 150;
    if(my_strlen($string > $length)
    {
    $string = preg_replace(‘/s+?(S+)?$/’, ”, substr($string, 0, $length));
    $string .= ‘…’; //
    }

    // the advantages, still displaying minimum long string that we need while limiting the maximum string, cut/triggered by space and followed by dots..

  • Alex

    It is indian coding.

    Instead of:
    if (strlen($string) $length) {
    //do stuff
    }