2012-07-04 12 views

Odpowiedz

195

Funkcja ta stworzy przyjazną SEO ciąg

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

powinno być dobrze :)

+3

+1 doskonały fragment:) – Mahdi

+1

Dziękuję. Jest to przyjemna i prosta funkcja. Byłoby miło rozszerzyć, aby usunąć niektóre nieprzyjazne słowa kluczowe, takie jak "the" & "and". – rorypicko

+0

z pewnością to naprawdę działa dobrze i warto go przedłużyć! – Mahdi

8

Wymiana szczególne znaki: http://se.php.net/manual/en/function.str-replace.php

Przykład:

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 i jeśli chcesz zająć się jakąś specjalną Charą cters musisz zadeklarować je we wzorcu, w przeciwnym razie mogą zostać wypłukane. Można zrobić to w ten sposób:

strtolower(preg_replace('/-+/', '-', preg_replace('/[^\wáéíóú]/', '-', $string))); 
Powiązane problemy