Esempio n. 1
0
 /**
  * Limits the words in a string. Words are separated by spaces.
  * If string is composed of a single word, it will be shortened
  * with the limit length function.
  *
  * @param   string $textString
  * @param   int    $textWordsLimit
  * @param   string $textPlaceholder
  * @return  bool|string Returns false if bad arguments were provided.
  */
 public static function textLimitWords($textString, $textWordsLimit = 5, $textPlaceholder = '...')
 {
     if (ValidateHelper::validString($textString) && ValidateHelper::validString($textPlaceholder) && ValidateHelper::validInt($textWordsLimit)) {
         $textStringToArray = explode(' ', $textString);
         $textShortString = '';
         if (count($textStringToArray) == 1) {
             // Assume 5 is the average word length.
             return self::textLimitLength($textString, $textWordsLimit * 5);
         } elseif (count($textStringToArray) <= $textWordsLimit) {
             return $textString;
         } else {
             for ($i = 0; $i < $textWordsLimit; $i++) {
                 $textShortString .= $textStringToArray[$i] . ' ';
             }
             // Removing the last \space\.
             return substr($textShortString, 0, -1) . $textPlaceholder;
         }
     } else {
         return false;
     }
 }