short() static public method

Shortens a string and adds an ellipsis if the string is too long
static public short ( string $string, integer $chars, string $rep = '…' ) : string
$string string The string to be shortened
$chars integer The final number of characters the string should have
$rep string The element, which should be added if the string is too long. Ellipsis is the default.
return string The shortened string
Example #1
0
function truncateString($text, $length)
{
    ob_start();
    $short = \str::short($text, $length);
    ob_end_clean();
    return $short;
}
Example #2
0
 public function testShort()
 {
     // too long
     $this->assertEquals('Super…', str::short($this->sample, 5));
     // not too long
     $this->assertEquals($this->sample, str::short($this->sample, 100));
     // zero chars
     $this->assertEquals($this->sample, str::short($this->sample, 0));
     // with different ellipsis character
     $this->assertEquals('Super---', str::short($this->sample, 5, '---'));
 }
Example #3
0
 /**
  * Shortens an URL
  * It removes http:// or https:// and uses str::short afterwards
  *
  * @param  string  $url The URL to be shortened
  * @param  int     $chars The final number of characters the URL should have
  * @param  boolean $base True: only take the base of the URL. 
  * @param  string  $rep The element, which should be added if the string is too long. Ellipsis is the default.
  * @return string  The shortened URL  
  */
 static function short($url, $chars = false, $base = false, $rep = '…')
 {
     $url = str_replace('http://', '', $url);
     $url = str_replace('https://', '', $url);
     $url = str_replace('ftp://', '', $url);
     $url = str_replace('www.', '', $url);
     if ($base) {
         $a = explode('/', $url);
         $url = a::get($a, 0);
     }
     return $chars ? str::short($url, $chars, $rep) : $url;
 }
Example #4
0
 * @param Field $field The calling Kirby Field instance
 * @param integer $chars The desired excerpt length
 * @return string
 */
field::$methods['excerpt'] = function ($field, $chars = 140, $mode = 'chars') {
    return excerpt($field, $chars, $mode);
};
/**
 * Shortens the field value by the given length
 * @param Field $field The calling Kirby Field instance
 * @param integer $length The desired string length
 * @param string $rep The attached ellipsis character if the string is longer
 * @return string
 */
field::$methods['short'] = function ($field, $length, $rep = '…') {
    return str::short($field->value, $length, $rep);
};
/**
 * Returns the string length of the field value
 * @param Field $field The calling Kirby Field instance
 * @return integer
 */
field::$methods['length'] = function ($field) {
    return str::length($field->value);
};
/**
 * Returns the word count for the field value
 * @param Field $field The calling Kirby Field instance
 * @return integer
 */
field::$methods['words'] = function ($field) {
Example #5
0
 /**
  * Shortens a URL
  * It removes http:// or https:// and uses str::short afterwards
  *
  * <code>
  *
  * echo url::short('http://veryveryverylongurl.com', 30);
  * // output: veryveryverylongurl.com
  *
  * </code>
  *
  * @param  string  $url The URL to be shortened
  * @param  int     $chars The final number of characters the URL should have
  * @param  boolean $base True: only take the base of the URL.
  * @param  string  $rep The element, which should be added if the string is too long. Ellipsis is the default.
  * @return string  The shortened URL
  */
 public static function short($url, $length = false, $base = false, $rep = '…')
 {
     if ($base) {
         $url = static::base($url);
     }
     // replace all the nasty stuff from the url
     $url = str_replace(array('http://', 'https://', 'ftp://', 'www.'), '', $url);
     // try to remove the last / after the url
     $url = rtrim($url, '/');
     return $length ? str::short($url, $length, $rep) : $url;
 }
Example #6
0
 /**
  * Shortens an URL
  * It removes http:// or https:// and uses str::short afterwards
  *
  * @param  string  $url The URL to be shortened
  * @param  int     $chars The final number of characters the URL should have
  * @param  boolean $base True: only take the base of the URL. 
  * @param  string  $rep The element, which should be added if the string is too long. Ellipsis is the default.
  * @return string  The shortened URL  
  */
 static function short($url, $chars = false, $base = false, $rep = '…')
 {
     $url = str_replace('http://', '', $url);
     $url = str_replace('https://', '', $url);
     $url = str_replace('ftp://', '', $url);
     $url = str_replace('www.', '', $url);
     if ($base) {
         $a = explode('/', $url);
         $url = a::get($a, 0);
     }
     // try to remove the last / after the url
     $url = preg_replace('!(\\/)$!', '', $url);
     return $chars ? str::short($url, $chars, $rep) : $url;
 }