Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new methods limit_characters and limit_words #55

Merged
merged 1 commit into from
Jan 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/utilphp/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,42 @@ public static function safe_truncate( $string, $length, $append = '...' )
return $ret;
}


/**
* Truncate the string to given length of charactes.
*
* @param $string
* @param $limit
* @param string $append
* @return string
*/
public static function limit_characters( $string, $limit = 100, $append = '...')
{
if (mb_strlen($string) <= $limit) {
return $string;
}

return rtrim(mb_substr($string, 0, $limit, 'UTF-8')).$append;
}

/**
* Truncate the string to given length of words.
*
* @param $string
* @param $limit
* @param string $append
* @return string
*/
public static function limit_words( $string, $limit = 100, $append = '...')
{
preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $string, $matches);
if ( ! isset($matches[0]) || strlen($string) === strlen($matches[0])) {
return $string;
}

return rtrim($matches[0]).$append;
}

/**
* Returns the ordinal version of a number (appends th, st, nd, rd).
*
Expand Down Expand Up @@ -2010,4 +2046,4 @@ protected static function mb_internal_encoding( $encoding = null )

return 'UTF-8';
}
}
}
21 changes: 20 additions & 1 deletion tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,25 @@ public function test_safe_truncate()
$this->assertEquals( 'The...', util::safe_truncate( 'The quick brown fox jumps over the lazy dog', 7 ) );
}

public function test_limit_characters()
{
$this->assertEquals( 'The quick brown fox jump...', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 24 ) );
$this->assertEquals( 'The quick brown fox jumps over the lazy dog', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 55 ) );
$this->assertEquals( 'Th...', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 2 ) );
$this->assertEquals( 'The...', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 3 ) );
$this->assertEquals( 'The qui...', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 7 ) );
$this->assertEquals( 'The quick brown fox jumps over the lazy dog', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 150 ) );
}

public function test_limit_words()
{
$this->assertEquals( 'The quick brown...', util::limit_words( 'The quick brown fox jumps over the lazy dog', 3 ) );
$this->assertEquals( 'The quick brown fox jumps...', util::limit_words( 'The quick brown fox jumps over the lazy dog', 5 ) );
$this->assertEquals( 'The...', util::limit_words( 'The quick brown fox jumps over the lazy dog', 1 ) );
$this->assertEquals( 'The quick brown fox jumps over the lazy dog', util::limit_words( 'The quick brown fox jumps over the lazy dog', 90 ) );
$this->assertEquals( 'The quick brown fox jumps over the...', util::limit_words( 'The quick brown fox jumps over the lazy dog', 7 ) );
}

public function test_ordinal()
{
$this->assertEquals( '1st', util::ordinal( 1 ) );
Expand Down Expand Up @@ -606,4 +625,4 @@ public function test_get_file_ext()
$expect = 'pdf';
$this->assertEquals($expect, Util::get_file_ext($input));
}
}
}