Beispiel #1
0
 public function truncate($config = array())
 {
     $config = new KConfig($config);
     $config->append(array('text' => '', 'offset' => 0, 'length' => 100, 'pad' => ' ...'));
     // Don't show endstring if actual string length is less than cutting length
     $config->endstr = KHelperString::strlen($config->text) < $config->length ? '' : $config->pad;
     return KHelperString::substr(strip_tags($config->text), $config->offset, $config->length) . $config->pad;
 }
Beispiel #2
0
 public function truncate($config = array())
 {
     $config = new KConfig($config);
     $config->append(array('text' => '', 'offset' => 0, 'length' => 100, 'path' => ' ...', 'allowed_tags' => ''));
     $text = strip_tags($config->text, $config->allowed_tags);
     if (KHelperString::strlen($config->text) > $config->length) {
         $text = KHelperString::substr($text, $config->offset, $config->length) . $config->path;
     }
     return $text;
 }
Beispiel #3
0
 /**
  * UTF-8 aware alternative to ucfirst
  *
  * Make a string's first character uppercase
  *
  * @param string
  * @return string with first character as upper case (if applicable)
  * @see http://www.php.net/ucfirst
  */
 public static function ucfirst($str)
 {
     switch (KHelperString::strlen($str)) {
         case 0:
             return '';
             break;
         case 1:
             return KHelperString::strtoupper($str);
             break;
         default:
             preg_match('/^(.{1})(.*)$/us', $str, $matches);
             return KHelperString::strtoupper($matches[1]) . $matches[2];
             break;
     }
 }
Beispiel #4
0
 /**
  * Validate length.
  *
  * @param KConfig $config Configuration. Contains keys property,value,entity
  *
  * @return bool Return true if it's valid or false if it's not
  */
 protected function _validateLength(KConfig $config)
 {
     $property = $config->property;
     $value = $config->value;
     $entity = $config->entity;
     $options = KConfig::unbox($config->options);
     //if a number is just passed then treat it as max
     if (!is_array($options)) {
         $options = array('max' => $options);
     }
     if ($property->isAttribute() && $property->isScalar()) {
         $options = KConfig::unbox($options);
         if (is_array($options)) {
             //check the min/max length
             if (isset($options['max']) || isset($options['min'])) {
                 if (isset($options['max'])) {
                     $greater = KHelperString::strlen($value) > (int) $options['max'];
                     if ($greater) {
                         $entity->addError(array('message' => sprintf(JText::_('%s %s can not be greater than %d characters'), $this->getIdentifier()->name, $property->getName(), $options['max']), 'code' => AnError::INVALID_LENGTH, 'key' => $property->getName(), 'max_lenght' => $options['max']));
                         return false;
                     }
                 }
                 if (isset($options['min'])) {
                     $lesser = KHelperString::strlen($value) < (int) $options['min'];
                     if ($lesser) {
                         $entity->addError(array('message' => sprintf(JText::_('%s %s can not be less than %d characters'), $this->getIdentifier()->name, $property->getName(), $options['min']), 'code' => AnError::INVALID_LENGTH, 'key' => $property->getName(), 'min_length' => $options['min']));
                         return false;
                     }
                 }
             }
         } else {
             if (KHelperString::strlen($value) != (int) $options) {
                 $entity->addError(array('message' => sprintf(JText::_('%s %s must be %d characters'), $this->getIdentifier()->name, $property->getName(), $options), 'code' => AnError::INVALID_LENGTH, 'key' => $property->getName(), 'length' => (int) $options));
                 return false;
             }
         }
     }
 }
Beispiel #5
0
 /**
  * returns substring of characters around a searchword.
  *
  * @param string The source string
  * @param int Number of chars to return
  * @param string The searchword to select around
  *
  * @return string
  */
 public function substring($text, $searchword, $length = 200)
 {
     $textlen = KHelperString::strlen($text);
     $lsearchword = KHelperString::strtolower($searchword);
     $wordfound = false;
     $pos = 0;
     while ($wordfound === false && $pos < $textlen) {
         if (($wordpos = @KHelperString::strpos($text, ' ', $pos + $length)) !== false) {
             $chunk_size = $wordpos - $pos;
         } else {
             $chunk_size = $length;
         }
         $chunk = KHelperString::substr($text, $pos, $chunk_size);
         $wordfound = KHelperString::strpos(KHelperString::strtolower($chunk), $lsearchword);
         if ($wordfound === false) {
             $pos += $chunk_size + 1;
         }
     }
     //while
     if ($wordfound !== false) {
         return ($pos > 0 ? '...&nbsp;' : '') . $chunk . '&nbsp;...';
     } else {
         if (($wordpos = @KHelperString::strpos($text, ' ', $length)) !== false) {
             return KHelperString::substr($text, 0, $wordpos) . '&nbsp;...';
         } else {
             return KHelperString::substr($text, 0, $length);
         }
     }
 }