From cde712ce354dcfd69f9c5e2b56ef7d6f1206d11a Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Fri, 9 Dec 2011 11:27:51 -0500 Subject: Added ability to change the separator value in the humanize function --- system/helpers/inflector_helper.php | 18 +++++++++--------- user_guide_src/source/changelog.rst | 1 + user_guide_src/source/helpers/inflector_helper.rst | 6 ++++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index 513a9c54f..3393bda8f 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -5,9 +5,9 @@ * An open source application development framework for PHP 5.1.6 or newer * * NOTICE OF LICENSE - * + * * Licensed under the Open Software License version 3.0 - * + * * This source file is subject to the Open Software License (OSL 3.0) that is * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: @@ -84,7 +84,7 @@ if ( ! function_exists('singular')) '/(n)ews$/' => '\1\2ews', '/([^u])s$/' => '\1', ); - + foreach ($singular_rules as $rule => $replacement) { if (preg_match($rule, $result)) @@ -115,7 +115,7 @@ if ( ! function_exists('plural')) function plural($str, $force = FALSE) { $result = strval($str); - + $plural_rules = array( '/^(ox)$/' => '\1\2en', // ox '/([m|l])ouse$/' => '\1ice', // mouse, louse @@ -196,20 +196,20 @@ if ( ! function_exists('underscore')) /** * Humanize * - * Takes multiple words separated by underscores and changes them to spaces + * Takes multiple words separated by the separator and changes them to spaces * * @access public - * @param string + * @param string $str + * @param string $separator * @return str */ if ( ! function_exists('humanize')) { - function humanize($str) + function humanize($str, $separator = '_') { - return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str)))); + return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str)))); } } - /* End of file inflector_helper.php */ /* Location: ./system/helpers/inflector_helper.php */ \ No newline at end of file diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 205b087f5..6f41f4519 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -33,6 +33,7 @@ Release Date: Not Released - url_title() will now trim extra dashes from beginning and end. - Added XHTML Basic 1.1 doctype to :doc:`HTML Helper `. + - Changed humanize to include a second param for the separator. - Database diff --git a/user_guide_src/source/helpers/inflector_helper.rst b/user_guide_src/source/helpers/inflector_helper.rst index cf246b9de..cc46a1851 100644 --- a/user_guide_src/source/helpers/inflector_helper.rst +++ b/user_guide_src/source/helpers/inflector_helper.rst @@ -77,3 +77,9 @@ them. Each word is capitalized. Example $word = "my_dog_spot"; echo humanize($word); // Returns "My Dog Spot" +To use dashes instead of underscores + +:: + + $word = "my-dog-spot"; + echo humanize($word, '-'); // Returns "My Dog Spot" \ No newline at end of file -- cgit v1.2.3-24-g4f1b