如何使用codeigniter url_title函数防止带加号?

时间:2012-10-31 11:31:07

标签: php codeigniter preg-replace str-replace stripslashes

我正在使用该功能以防止错误的URL结构:

    <?
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('seo_helper')) {

function seo_link($deger) {
$turkce=array("ş","Ş","ı","(",")","'","ü","Ü","ö","Ö","ç","Ç"," ","/","*","?","ş","Ş","ı","ğ","Ğ","İ","ö","Ö","Ç","ç","ü","Ü");
$duzgun=array("s","S","i","","","","u","U","o","O","c","C","-","-","-","","s","S","i","g","G","I","o","O","C","c","u","U");
$deger=str_replace($turkce,$duzgun,$deger);
$deger = url_title_plus($deger);
return $deger;
}  

}  


if ( ! function_exists('url_title_plus'))
{
    function url_title_plus($str, $separator = 'dash', $lowercase = FALSE)
    {
        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);
        }

        if ($lowercase === TRUE)
        {
            $str = strtolower($str);
        }

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

输入字符串:

ATA'DAN MEZİTLİDE 4+1 HAVUZLU GÜVENLİKLİ KAÇMAZ!!!

输出:

ATADAN-MEZITLIDE-41-HAVUZLU-GUVENLIKLI-KACMAZ

我想要的是什么:

ATADAN-MEZITLIDE-4+1-HAVUZLU-GUVENLIKLI-KACMAZ

1 个答案:

答案 0 :(得分:1)

替换这部分代码并进行检查: -

'[^a-z0-9\-\+\._]'        => '',

它的作用是允许加号(+)。而不是用''。

替换它