From 3edd88eee84886fc6ba3e1fc25beda3c424370bc Mon Sep 17 00:00:00 2001 From: tubalmartin Date: Sat, 3 Mar 2012 22:10:34 +0100 Subject: An even better url_title helper. Tests: http://codepad.org/tuJgvkyN Changelog entry added for 2.1.1 --- system/helpers/url_helper.php | 27 +++++++-------------------- user_guide/changelog.html | 5 +++++ 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php index 9f4b85248..cdb6dae9c 100644 --- a/system/helpers/url_helper.php +++ b/system/helpers/url_helper.php @@ -478,27 +478,14 @@ if ( ! function_exists('url_title')) { function url_title($str, $separator = 'dash', $lowercase = FALSE) { - if ($separator == 'dash') - { - $search = '_'; - $replace = '-'; - } - else - { - $search = '-'; - $replace = '_'; - } + $replace = $separator == 'dash' ? '-' : '_'; $trans = array( - '&\#\d+?;' => '', - '&\S+?;' => '', - '\s+' => $replace, - '[^a-z0-9\-\._]' => '', - $replace.'+' => $replace, - $replace.'$' => $replace, - '^'.$replace => $replace, - '\.+$' => '' - ); + '&.+?;' => '', + '[^a-z0-9 _-]' => '', + '\s+' => $replace, + $replace.'+' => $replace + ); $str = strip_tags($str); @@ -512,7 +499,7 @@ if ( ! function_exists('url_title')) $str = strtolower($str); } - return trim(stripslashes($str)); + return trim($str, $replace); } } diff --git a/user_guide/changelog.html b/user_guide/changelog.html index b87e61ba8..b85dc8d52 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -71,6 +71,11 @@ Change Log
  • Further improved MIME type detection in the File Uploading Library.
  • +
  • Helpers + +
  • Bug fixes for 2.1.1

    -- cgit v1.2.3-24-g4f1b From 1a6971030718e2e92e6fc80750f7a14faf035257 Mon Sep 17 00:00:00 2001 From: tubalmartin Date: Sun, 4 Mar 2012 16:01:11 +0100 Subject: Allow developers to use any string as a separator, not just dashes or underscores. Backwards compatible when using 'dash' or 'underscore' as string separator. Tests: http://codepad.org/DWcxVH5r --- system/helpers/url_helper.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php index cdb6dae9c..f1e8c6ac6 100644 --- a/system/helpers/url_helper.php +++ b/system/helpers/url_helper.php @@ -466,25 +466,34 @@ if ( ! function_exists('prep_url')) * Create URL Title * * Takes a "title" string as input and creates a - * human-friendly URL string with either a dash - * or an underscore as the word separator. + * human-friendly URL string with a "separator" string + * as the word separator. * * @access public * @param string the string - * @param string the separator: dash, or underscore + * @param string the separator * @return string */ if ( ! function_exists('url_title')) { - function url_title($str, $separator = 'dash', $lowercase = FALSE) + function url_title($str, $separator = '-', $lowercase = FALSE) { - $replace = $separator == 'dash' ? '-' : '_'; + if ($separator == 'dash') + { + $separator = '-'; + } + else if ($separator == 'underscore') + { + $separator = '_'; + } + + $q_separator = preg_quote($separator); $trans = array( - '&.+?;' => '', - '[^a-z0-9 _-]' => '', - '\s+' => $replace, - $replace.'+' => $replace + '&.+?;' => '', + '[^a-z0-9 _-]' => '', + '\s+' => $separator, + '('.$q_separator.')+' => $separator ); $str = strip_tags($str); @@ -499,7 +508,7 @@ if ( ! function_exists('url_title')) $str = strtolower($str); } - return trim($str, $replace); + return trim($str, $separator); } } -- cgit v1.2.3-24-g4f1b From a67ddc61937d178231607f66f2af3ef729f208d4 Mon Sep 17 00:00:00 2001 From: tubalmartin Date: Sun, 4 Mar 2012 16:18:04 +0100 Subject: User guide updated --- user_guide/helpers/url_helper.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/user_guide/helpers/url_helper.html b/user_guide/helpers/url_helper.html index 76ec32ab2..c23c5ac92 100644 --- a/user_guide/helpers/url_helper.html +++ b/user_guide/helpers/url_helper.html @@ -231,11 +231,11 @@ $url_title = url_title($title);
    -

    The second parameter determines the word delimiter. By default dashes are used. Options are: dash, or underscore:

    +

    The second parameter determines the word delimiter. By default dashes are used.

    $title = "What's wrong with CSS?";

    -$url_title = url_title($title, 'underscore');
    +$url_title = url_title($title, '_');

    // Produces: Whats_wrong_with_CSS
    @@ -244,7 +244,7 @@ $url_title = url_title($title, 'underscore');
    $title = "What's wrong with CSS?";

    -$url_title = url_title($title, 'underscore', TRUE);
    +$url_title = url_title($title, '_', TRUE);

    // Produces: whats_wrong_with_css
    -- cgit v1.2.3-24-g4f1b From 6f6897cdce965e1cf54deb40ce93ba9be281108f Mon Sep 17 00:00:00 2001 From: tubalmartin Date: Sun, 4 Mar 2012 16:24:04 +0100 Subject: Changelog updated --- user_guide/changelog.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide/changelog.html b/user_guide/changelog.html index b85dc8d52..613c4e65d 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -73,7 +73,7 @@ Change Log
  • Helpers
      -
    • url_title() performance and output improved.
    • +
    • url_title() performance and output improved. You can now use any string as the word delimiter. Backwards compatible with 'dash' or 'underscore' as words delimiters.
  • -- cgit v1.2.3-24-g4f1b