Skip to content

Commit

Permalink
Merge pull request #42 from jmontoyaa/patch-1
Browse files Browse the repository at this point in the history
Treat underscore as space option
  • Loading branch information
jbroadway authored Jul 27, 2016
2 parents 8126e75 + f2a3509 commit 984c991
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions URLify.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ public static function downcode ($text, $language = "") {
* @param bool $file_name Whether there should be and additional filter considering this is a filename
* @param bool $use_remove_list Whether you want to remove specific elements previously set in self::$remove_list
* @param bool $lower_case Whether you want the filter to maintain casing or lowercase everything (default)
* @param bool $treat_underscore_as_space Treat underscore as space, so it will replaced with "-"
*/
public static function filter ($text, $length = 60, $language = "", $file_name = false, $use_remove_list = true, $lower_case = true) {
public static function filter ($text, $length = 60, $language = "", $file_name = false, $use_remove_list = true, $lower_case = true, $treat_underscore_as_space = true) {
$text = self::downcode ($text,$language);

if ($use_remove_list) {
Expand All @@ -251,7 +252,9 @@ public static function filter ($text, $length = 60, $language = "", $file_name =
// if downcode doesn't hit, the char will be stripped here
$remove_pattern = ($file_name) ? '/[^_\-.\-a-zA-Z0-9\s]/u' : '/[^\s_\-a-zA-Z0-9]/u';
$text = preg_replace ($remove_pattern, '', $text); // remove unneeded chars
$text = str_replace ('_', ' ', $text); // treat underscores as spaces
if ($treat_underscore_as_space) {
$text = str_replace ('_', ' ', $text); // treat underscores as spaces
}
$text = preg_replace ('/^\s+|\s+$/u', '', $text); // trim leading/trailing spaces
$text = preg_replace ('/[-\s]+/u', '-', $text); // convert spaces to hyphens
if ($lower_case) {
Expand Down
2 changes: 2 additions & 0 deletions tests/URLifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function test_filter () {
$this->assertEquals ('bobby-mcferrin-dont-worry-be-happy', URLify::filter ("Bobby McFerrin — Don't worry be happy",600,"en"));
// test stripping and conversion of UTF-8 spaces
$this->assertEquals ('test-mahito-mukai', URLify::filter('向井 真人test (Mahito Mukai)'));
// Treat underscore as space
$this->assertEquals ('text_with_underscore', URLify::filter('text_with_underscore', 60, "en", true, true, true, false));
}

function test_add_chars () {
Expand Down

0 comments on commit 984c991

Please sign in to comment.