Skip to content

Commit

Permalink
string tuncation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
azt3k committed May 23, 2014
1 parent af6750e commit 16a1d07
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions src/abc/code/Classes/AbcStr.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
<?php

class AbcStr{

public $str = '';
public $originalStr = '';
public static $wordLimit = 50;
public static $charLimit = 300;

public function __construct($str) {
$this->str = $str;
$this->originalStr = $str;
}

public static function get($str) {
return new self($str);
}

public function limitWords($wordLimit = null, $overflowIndicator = '...') {
if (!$wordLimit) $wordLimit = self::$wordLimit;
$words = explode(" ",$this->str);
$this->str = implode(" ",array_splice($words,0,$wordLimit)).$overflowIndicator;
return $this;
}

public function limitChars($charLimit = null, $overflowIndicator = '...') {
if (!$charLimit) $charLimit = self::$charLimit;
if (strlen($this->str) <= $charLimit) return $this;
$effectiveLimit = $charLimit - strlen($overflowIndicator);
$this->str = substr($this->str, 0, $effectiveLimit).$overflowIndicator;
return $this;
}

public function limitCharsNoDotDot($charLimit = null) {
$this->limitChars($charLimit = null, $overflowIndicator = '');
return $this;
}

public function __toString() {
return $this->str;
}

<?php

class AbcStr{

public $str = '';
public $originalStr = '';
public static $wordLimit = 50;
public static $charLimit = 300;

public function __construct($str) {
$this->str = $str;
$this->originalStr = $str;
}

public static function get($str) {
return new self($str);
}

public function limitWords($wordLimit = null, $overflowIndicator = '...') {
if (!$wordLimit) $wordLimit = self::$wordLimit;
$words = explode(" ",$this->str);
if (count($words) <= $wordLimit) $overflowIndicator = '';
$this->str = implode(" ",array_splice($words,0,$wordLimit)).$overflowIndicator;
return $this;
}

public function limitChars($charLimit = null, $overflowIndicator = '...') {
if (!$charLimit) $charLimit = self::$charLimit;
if (strlen($this->str) <= $charLimit) return $this;
$effectiveLimit = $charLimit - strlen($overflowIndicator);
$this->str = substr($this->str, 0, $effectiveLimit).$overflowIndicator;
return $this;
}

public function limitCharsNoDotDot($charLimit = null) {
$this->limitChars($charLimit = null, $overflowIndicator = '');
return $this;
}

public function __toString() {
return $this->str;
}

}

0 comments on commit 16a1d07

Please sign in to comment.