diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-09-08 20:03:55 -0700 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-09-08 20:03:55 -0700 |
commit | 48becbe017758fb6fb95f0806ece879764249f37 (patch) | |
tree | 60a599bdb68c7bc23d97125ff5b665e8b8cd79ce | |
parent | 453fb76443898746b72a0a2ddd419cf8e54f4207 (diff) |
Update item::convert_filename_to_slug() to eliminate leading and
trailing hyphens.
-rw-r--r-- | modules/gallery/helpers/item.php | 6 | ||||
-rw-r--r-- | modules/gallery/tests/Item_Helper_Test.php | 5 |
2 files changed, 10 insertions, 1 deletions
diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index d907a177..e015cb72 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -113,7 +113,11 @@ class item_Core { * @param string $filename */ static function convert_filename_to_slug($filename) { - return preg_replace("/[^A-Za-z0-9-_]+/", "-", pathinfo($filename, PATHINFO_FILENAME)); + $result = pathinfo($filename, PATHINFO_FILENAME); + $result = preg_replace("/[^A-Za-z0-9-_]+/", "-", $result); + $result = preg_replace("/^-+/", "", $result); + $result = preg_replace("/-+$/", "", $result); + return $result; } /** diff --git a/modules/gallery/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index 87859565..33fcdb73 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -49,6 +49,11 @@ class Item_Helper_Test extends Unit_Test_Case { $this->assert_equal(1, $input->not_url_safe); } + public function convert_filename_to_slug_test() { + $this->assert_equal("foo", item::convert_filename_to_slug("{[foo]}")); + $this->assert_equal("foo-bar", item::convert_filename_to_slug("{[foo!@#!$@#^$@($!(@bar]}")); + } + private static function _create_random_item($album) { // Set all required fields (values are irrelevant) $item = ORM::factory("item"); |