2012-07-04 22 views

Respuesta

195

Esta función creará una cadena SEO friendly

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; 
} 

debe estar bien :)

+3

1 fragmento perfecta:) – Mahdi

+1

Gracias. Es una función sencilla y agradable. Sería bueno extenderse para eliminar ciertas palabras clave poco amistosas como 'the' & 'y'. – rorypicko

+0

seguro, realmente funciona bien y vale la pena extenderlo! – Mahdi

8

Sustitución de caracteres específicos: http://se.php.net/manual/en/function.str-replace.php

Ejemplo:

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; 
} 
6

Yop, y si quieres manejar cualquier chara especial cters tendrá que declararlos en el patrón, de lo contrario, pueden ser eliminados. Es posible hacerlo de esa manera:

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