CodeIgniter Forums
Disallowing periods etc in url_title() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Disallowing periods etc in url_title() (/thread-18464.html)



Disallowing periods etc in url_title() - El Forum - 05-07-2009

[eluser]rt30000[/eluser]
How should I go about removing periods (and preferably empty trailing spaces as well) with the CI url_title() helper function? Below is the CI helper function code. Also, shouldn't I create my own helper rather than modify the system one? I have seen references to this, but have yet to extend or modify core files. I will have to look up this, if so. Thanks!!

Code:
// ------------------------------------------------------------------------

/**
* 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.
*
* @access    public
* @param    string    the string
* @param    string    the separator: dash, or underscore
* @return    string
*/
if ( ! function_exists('url_title'))
{
    function url_title($str, $separator = 'dash')
    {
        if ($separator == 'dash')
        {
            $search        = '_';
            $replace    = '-';
        }
        else
        {
            $search        = '-';
            $replace    = '_';
        }

        $trans = array(
                        '&\#\d+?;'                => '',
                        '&\S+?;'                => '',
                        '\s+'                    => $replace,
                        '[^a-z0-9\-\._]'        => '',
                        $replace.'+'            => $replace,
                        $replace.'$'            => $replace,
                        '^'.$replace            => $replace
                      );

        $str = strip_tags($str);

        foreach ($trans as $key => $val)
        {
            $str = preg_replace("#".$key."#i", $val, $str);
        }

        return trim(stripslashes($str));
    }
}

// ------------------------------------------------------------------------