@@ 531-545 (lines=15) @@ | ||
528 | * @param int $index The index at which to insert the substring |
|
529 | * @return Stringy Object with the resulting $str after the insertion |
|
530 | */ |
|
531 | public function insert($substring, $index) |
|
532 | { |
|
533 | $stringy = static::create($this->str, $this->encoding); |
|
534 | if ($index > $stringy->length()) { |
|
535 | return $stringy; |
|
536 | } |
|
537 | ||
538 | $start = mb_substr($stringy->str, 0, $index, $stringy->encoding); |
|
539 | $end = mb_substr($stringy->str, $index, $stringy->length(), |
|
540 | $stringy->encoding); |
|
541 | ||
542 | $stringy->str = $start . $substring . $end; |
|
543 | ||
544 | return $stringy; |
|
545 | } |
|
546 | ||
547 | /** |
|
548 | * Returns true if the string contains only alphabetic chars, false |
|
@@ 1509-1524 (lines=16) @@ | ||
1506 | * @param string $substring The substring to append if it can fit |
|
1507 | * @return Stringy Object with the resulting $str after truncating |
|
1508 | */ |
|
1509 | public function truncate($length, $substring = '') |
|
1510 | { |
|
1511 | $stringy = static::create($this->str, $this->encoding); |
|
1512 | if ($length >= $stringy->length()) { |
|
1513 | return $stringy; |
|
1514 | } |
|
1515 | ||
1516 | // Need to further trim the string so we can append the substring |
|
1517 | $substringLength = mb_strlen($substring, $stringy->encoding); |
|
1518 | $length = $length - $substringLength; |
|
1519 | ||
1520 | $truncated = mb_substr($stringy->str, 0, $length, $stringy->encoding); |
|
1521 | $stringy->str = $truncated . $substring; |
|
1522 | ||
1523 | return $stringy; |
|
1524 | } |
|
1525 | ||
1526 | /** |
|
1527 | * Returns a lowercase and trimmed string separated by underscores. |