Strip php variable, replace white spaces with dashes

Strip php variable, replace white spaces with dashes

This function will create an SEO friendly string

function seoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    //Make alphanumeric (removes all other characters)
    $string = preg_replace(/[^a-z0-9_s-]/, , $string);
    //Clean up multiple dashes or whitespaces
    $string = preg_replace(/[s-]+/,  , $string);
    //Convert whitespaces and underscore to dash
    $string = preg_replace(/[s_]/, -, $string);
    return $string;
}

should be fine 🙂

Yop, and if you want to handle any special characters youll need to declare them in the pattern, otherwise they may get flushed out. You may do it that way:

strtolower(preg_replace(/-+/, -, preg_replace(/[^wáéíóú]/, -, $string)));

Strip php variable, replace white spaces with dashes

Replacing specific characters:
http://se.php.net/manual/en/function.str-replace.php

Example:

function replaceAll($text) { 
    $text = strtolower(htmlentities($text)); 
    $text = str_replace(get_html_translation_table(), -, $text);
    $text = str_replace( , -, $text);
    $text = preg_replace(/[-]+/i, -, $text);
    return $text;
}

Leave a Reply

Your email address will not be published.