jay
jay

Reputation: 10325

Allow dot in php preg_replace

I'm using The perfect PHP clean url generator to clean some file names with an upload script (as well as to clean my permalinks elsewhere). How might I modify it to allow a . if the function looks like this:

setlocale(LC_ALL, 'en_US.UTF8');
function toAscii($str, $replace=array(), $delimiter='-', $exception = '') {
    if( !empty($replace) ) {
        $str = str_replace((array)$replace, ' ', $str);
    }

    $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
    $clean = strtolower(trim($clean, '-'));
    $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

    return $clean;
}

Thanks in advance.

EDIT Modified the function slightly to reflect my actual usage - sorry about that.

Upvotes: 2

Views: 9306

Answers (1)

Justin Morgan
Justin Morgan

Reputation: 30700

So you just want to avoid replacing literal . characters, is that correct? The dot should be interpreted literally if it's inside a character class, so your first regex line would just become:

$clean = preg_replace("/[^a-zA-Z0-9\/_|+ .-]/", '', $clean);

Upvotes: 10

Related Questions