public function testSubstr() { $this->assertEquals('ä', \Beluga\substr('ä…', 0, 1), 'Assert 1 fails'); $this->assertEquals('ä', \Beluga\substr('ä…', 0, -1), 'Assert 2 fails'); $this->assertEquals('…', \Beluga\substr('ä…', 1), 'Assert 3 fails'); $this->assertEquals('…', \Beluga\substr('ä…', 1, 2), 'Assert 4 fails'); $this->assertEquals('äAö', \Beluga\substr('äAöÖüÜß', 0, -4), 'Assert 5 fails'); $this->assertEquals('ö', \Beluga\substr('äAöÖüÜß', 2, -4), 'Assert 6 fails'); $this->assertEquals('·…', \Beluga\substr('»«¢„“”µ·…–ĸŋđðſæ@ł€¶ŧ←↓→øþ', 7, 2), 'Assert 7 fails'); }
/** * Trim the string $str to a max. length and appends, if trimming is required, a $appendix. * * <code> * // Example * * $str = 'My name is Max'; * $newStr = \Beluga\strMax( $str, 11, 'Art' ); * echo "'{$newStr}'"; * * // Results in output: 'My name is Art' * </code> * * @param string $str The string. * @param integer $maxLength The resulting string max. length (incl. $appendix) * @param string $appendix Optional appendix (Defaults to '…') * @param string $charset Encoding of the string (defaults to 'UTF-8') * @return string * @uses \Beluga\strLen * @uses \Beluga\substr * @since v0.1 */ function strMax(string $str, int $maxLength, string $appendix = '…', string $charset = 'UTF-8') : string { // If no trimming is required return the origin string if (strLen($str, $charset) <= $maxLength) { return $str; } return \Beluga\substr($str, 0, $maxLength - strLen($appendix, $charset)) . $appendix; }