Benim Seo url jeneratör geliştirmek için nasıl

5 Cevap php

i bir dizeden bir seo dostu url oluşturur bu işlevi vardır:

 function seo_titleinurl_generate($title)
         {


            $title=substr($title,0,160);

            $title = ereg_replace(" ", "-", $title); // replace spaces by "-"

            $title = ereg_replace("á", "a", $title); // replace special chars

            $title = ereg_replace("í", "i", $title); // replace special chars

            $title = ereg_replace("ó", "o", $title); // replace special chars

            $title = ereg_replace("ú", "u", $title); // replace special chars

            $title = ereg_replace("ñ", "n", $title); // replace special chars

            $title = ereg_replace("Ñ", "n", $title); // replace special chars

            $title = strtolower(trim($title)); // lowercase
            $title = preg_replace("/([^a-zA-Z0-9_-])/",'',$title); // only keep  standard latin letters and numbers, hyphens and dashes

           if($title=="" or $title=="-"){
           $mr=rand(1,99999);
           $mt=time();
           $title=$mr.$mt;
         }

             return $title;
     }

But in some cases when the string has multiple spaces like : the most (3 spaces here) nice pranks! it's generates : the-most---nice-pranks

i birçok alanlarda göz ardı ve onları tek bir çizgi yapmak istiyorum.

Teşekkürler

5 Cevap

Ben bu biraz daha hızlı önceki cevabı daha olabileceğini düşünüyorum (yanlış olabilir) tek boşluklar etrafında karışıklık olmaz çünkü:

$title = preg_replace('/\s\s+/', ' ', $title);

Basitçe başında ekleyin:

$title = ereg_replace(/\s+/, " ", $title); 

Ereg_replace önerilmemektedir çünkü bu preg_replace kullanır ve PHP 6 uzağa gidiyor Ayrıca fonksiyon çağrıları ve bir-bir yedek (bu hızlı) için str_replace sayısını azaltmak için dizileri kullanır.:

function seo_titleinurl_generate($title)
{
        $title = substr(strtolower(trim($title)),0,160);

        $title = preg_replace('/\s+/', '-', $title); // replace spaces by "-"
        $title = str_replace(array("á","í","ó","ú","ñ","Ñ"), array("a","i","o","u","n","n"), $title);// replace special chars
        $title = preg_replace('/\W-/', '', $title); // only keep  standard latin letters and numbers, hyphens and dashes

        if($title=="" or $title=="-"){
            $mr=rand(1,99999);
            $mt=time();
            $title=$mr.$mt;
        }
        return $title;
 } 

Ben şu öneririm:

/**
* Produce a title with lowercase alphanumeric characters, underscores, 
* and dashes.  There should be no instances of multiple concurrent dashes, 
* and no spaces.
*
* @param string $title the title being sanitized
*
* @return string the sanitized title, or a concatenation of a random 
*                number and the current time
*/
function seoTitleInUrlGenerate($title)
{
    $title = substr( 
                 preg_replace(
                     array("/([^a-zA-Z0-9_-])/", "/([--]{2,})+/"),
                     array('', '-'),
                     strtolower( strtr( trim($title), 'áéíóúñÑ ', 'aeiounN-' ) )
                 ), 0, 160
             );

    if ($title == "" or $title == "-")
    {
        return rand(1, 99999) . time();
    }
    else
    {
        return $title;
    }
 }

Eğer sağlanan girişi ile test edildiğinde ...

echo seoTitleInUrlGenerate('the most    nice pranks!'); // "the-most-nice-pranks"

Eğer URL kullanmak için geçerli bir başlık üretmek mümkün olmasa Aksine rasgele sayı ve zamanı dönen daha, ben YANLIŞ dönen öneririm. Bu şekilde belki bir yere geçersiz başlık kaydını yapabilir ve daha sonra bunu düzeltmek. Şimdi olduğu gibi fonksiyonu ile, sadece sayısal bir dönüş değeri almak ve geçersiz bir başlık sonucudur ya da eğer bu sayıların tam olarak ne oldu, geçerli bir başlık olmadığını. Gerçekten bilmiyorum

Aşağıdaki kodu bir göz atın:

function Slug($string)
{
    return strtolower(trim(preg_replace(array('~[^0-9a-z]~i', '~-+~'), '-', preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'))), '-'));
}