Geliştirilmiş Seo Url Fonksiyonu Nedir?

sametuzun
Mesajlar: 507
Kayıt: Prş Haz 09, 2016 12:48 am

Geliştirilmiş Seo Url Fonksiyonu Nedir?

Mesajgönderen sametuzun » Çrş Haz 29, 2016 8:04 pm

url_slug fonksiyonu:

Kod: Tümünü seç

<?php 

/** 

 * Create a web friendly URL slug from a string. 

 *  

 * Although supported, transliteration is discouraged because 

 *     1) most web browsers support UTF-8 characters in URLs 

 *     2) transliteration causes a loss of information 

 * 

 * @author Sean Murphy <sean@iamseanmurphy.com> 

 * @copyright Copyright 2012 Sean Murphy. All rights reserved. 

 * @license http://creativecommons.org/publicdomain/zero/1.0/ 

 * 

 * @param string $str 

 * @param array $options 

 * @return string 

 */ 

function url_slug($str, $options = array()) { 

    // Make sure string is in UTF-8 and strip invalid UTF-8 characters 

    $str = mb_convert_encoding((string)$str, 'UTF-8', mb_list_encodings()); 

     

    $defaults = array( 

        'delimiter' => '-', 

        'limit' => null, 

        'lowercase' => true, 

        'replacements' => array(), 

        'transliterate' => false, 

    ); 

     

    // Merge options 

    $options = array_merge($defaults, $options); 

     

    $char_map = array( 

        // Latin 

        'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C',  

        'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',  

        'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O',  

        'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Å°' => 'U', 'Ý' => 'Y', 'Þ' => 'TH',  

        'ß' => 'ss',  

        'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c',  

        'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',  

        'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'Å‘' => 'o',  

        'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',  

        'ÿ' => 'y', 

  

        // Latin symbols 

        '©' => '(c)', 

  

        // Greek 

        'Α' => 'A', 'Î’' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'ÎËÅ“' => '8', 

        'Îâ„¢' => 'I', 'ÎÅ¡' => 'K', 'Λ' => 'L', 'ÎÅ“' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Î*' => 'P', 

        'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W', 

        'Îâ€*' => 'A', 'ÎËâ€*' => 'E', 'ÎÅ*' => 'I', 'ÎÅ’' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I', 

        'Ϋ' => 'Y', 

        'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8', 

        'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'Ï€' => 'p', 

        'ρ' => 'r', 'ÏÆ’' => 's', 'Ï„' => 't', 'Ï…' => 'y', 'Ïâ€*' => 'f', 'χ' => 'x', 'ÏËâ€*' => 'ps', 'ω' => 'w', 

        'ά' => 'a', 'Î*' => 'e', 'ί' => 'i', 'ÏÅ’' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'Ï‚' => 's', 

        'ÏÅ*' => 'i', 'ΰ' => 'y', 'Ï‹' => 'y', 'ΐ' => 'i', 

  

        // Turkish 

        'Ş' => 'S', 'İ' => 'I', 'Ç' => 'C', 'Ü' => 'U', 'Ö' => 'O', 'Ğ' => 'G', 

        'ş' => 's', 'ı' => 'i', 'ç' => 'c', 'ü' => 'u', 'ö' => 'o', 'ğ' => 'g',  

  

        // Russian 

        'А' => 'A', 'Ğ‘' => 'B', 'Ğ’' => 'V', 'Ğ“' => 'G', 'Ğ”' => 'D', 'Ğ•' => 'E', 'Ё' => 'Yo', 'Ğ–' => 'Zh', 

        'Ğ—' => 'Z', 'ĞËÅ“' => 'I', 'Ğâ„¢' => 'J', 'ĞÅ¡' => 'K', 'Ğ›' => 'L', 'ĞÅ“' => 'M', 'Н' => 'N', 'О' => 'O', 

        'ĞŸ' => 'P', 'Ğ*' => 'R', 'Ğ¡' => 'S', 'Ğ¢' => 'T', 'Ğ£' => 'U', 'Ф' => 'F', 'Ğ¥' => 'H', 'Ц' => 'C', 

        'Ч' => 'Ch', 'Ш' => 'Sh', 'Ğ©' => 'Sh', 'Ъ' => '', 'Ğ«' => 'Y', 'Ь' => '', 'Ğ*' => 'E', 'Ğ®' => 'Yu', 

        'Я' => 'Ya', 

        'Ğ°' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'Ğ´' => 'd', 'е' => 'e', 'Ñ‘' => 'yo', 'ж' => 'zh', 

        'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 

        'Ğ¿' => 'p', 'Ñ€' => 'r', 'с' => 's', 'Ñ‚' => 't', 'ÑÆ’' => 'u', 'Ñ„' => 'f', 'Ñ…' => 'h', 'Ñâ€*' => 'c', 

        'ч' => 'ch', 'ÑËâ€*' => 'sh', 'щ' => 'sh', 'ÑÅ*' => '', 'Ñ‹' => 'y', 'ÑÅ’' => '', 'э' => 'e', 'ю' => 'yu', 

        'я' => 'ya', 

  

        // Ukrainian 

        'Ğ„' => 'Ye', 'Ğâ€*' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 

        'Ñ”' => 'ye', 'Ñ–' => 'i', 'Ñ—' => 'yi', 'Ò‘' => 'g', 

  

        // Czech 

        'ÄÅ’' => 'C', 'Ď' => 'D', 'ÄÅ¡' => 'E', 'Ň' => 'N', 'ÅËÅ“' => 'R', 'Å*' => 'S', 'Ť' => 'T', 'Å®' => 'U',  

        'Ž' => 'Z',  

        'č' => 'c', 'ď' => 'd', 'Ä›' => 'e', 'ÅËâ€*' => 'n', 'Åâ„¢' => 'r', 'Å¡' => 's', 'Å¥' => 't', 'ů' => 'u', 

        'ž' => 'z',  

  

        // Polish 

        'Ä„' => 'A', 'Äâ€*' => 'C', 'ÄËÅ“' => 'e', 'Ł' => 'L', 'ÅÆ’' => 'N', 'Ó' => 'o', 'ÅÅ¡' => 'S', 'Ź' => 'Z',  

        'Å»' => 'Z',  

        'Ä…' => 'a', 'ć' => 'c', 'Äâ„¢' => 'e', 'Å‚' => 'l', 'Å„' => 'n', 'ó' => 'o', 'Å›' => 's', 'ź' => 'z', 

        'ż' => 'z', 

  

        // Latvian 

        'Ä€' => 'A', 'ÄÅ’' => 'C', 'Ä’' => 'E', 'Ä¢' => 'G', 'Ī' => 'i', 'Ķ' => 'k', 'Ä»' => 'L', 'Å…' => 'N',  

        'Å*' => 'S', 'Ū' => 'u', 'Ž' => 'Z', 

        'ā' => 'a', 'č' => 'c', 'Ä“' => 'e', 'Ä£' => 'g', 'Ä«' => 'i', 'Ä·' => 'k', 'ļ' => 'l', 'Åâ€*' => 'n', 

        'š' => 's', 'ū' => 'u', 'ž' => 'z' 

    ); 

     

    // Make custom replacements 

    $str = preg_replace(array_keys($options['replacements']), $options['replacements'], $str); 

     

    // Transliterate characters to ASCII 

    if ($options['transliterate']) { 

        $str = str_replace(array_keys($char_map), $char_map, $str); 

    } 

     

    // Replace non-alphanumeric characters with our delimiter 

    $str = preg_replace('/[^\p{L}\p{Nd}]+/u', $options['delimiter'], $str); 

     

    // Remove duplicate delimiters 

    $str = preg_replace('/(' . preg_quote($options['delimiter'], '/') . '){2,}/', '$1', $str); 

     

    // Truncate slug to max. characters 

    $str = mb_substr($str, 0, ($options['limit'] ? $options['limit'] : mb_strlen($str, 'UTF-8')), 'UTF-8'); 

     

    // Remove delimiter from ends 

    $str = trim($str, $options['delimiter']); 

     

    return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str; 

}
ÖRNEK KULLANIMI

Kod: Tümünü seç

<?php 

include('url_slug.php'); 

header('Content-type: text/plain; charset=utf-8'); 

  

// Basic usage 

echo "This is an example string. Nothing fancy." . "\n"; 

echo url_slug("This is an example string. Nothing fancy.") . "\n\n"; 

  

// Example using French with unwanted characters ('?) 

echo "Qu'en est-il français? Ça marche alors?" . "\n"; 

echo url_slug("Qu'en est-il français? Ça marche alors?") . "\n\n"; 

  

// Example using transliteration 

echo "ЧÑ‚о делаÑ‚ÑÅ’, если я не Ñ…очÑÆ’, UTF-8?" . "\n"; 

echo url_slug("ЧÑ‚о делаÑ‚ÑÅ’, если я не Ñ…очÑÆ’, UTF-8?", array('transliterate' => true)) . "\n\n"; 

  

// Example using transliteration on an unsupported language 

echo "מ×” אם א×*×â„¢ ×Å“א רוצ×” UTF-8 תווים?" . "\n"; 

echo url_slug("מ×” אם א×*×â„¢ ×Å“א רוצ×” UTF-8 תווים?", array('transliterate' => true)) . "\n\n"; 

  

// Some other options 

echo "This is an Example String. What's Going to Happen to Me?" . "\n"; 

echo url_slug( 

    "This is an Example String. What's Going to Happen to Me?",  

    array( 

        'delimiter' => '_', 

        'limit' => 40, 

        'lowercase' => false, 

        'replacements' => array( 

            '/\b(an)\b/i' => 'a', 

            '/\b(example)\b/i' => 'Test' 

        ) 

    ) 

); 

  

/* 

Output: 

  

This is an example string. Nothing fancy. 

this-is-an-example-string-nothing-fancy 

  

Qu'en est-il français? Ça marche alors? 

qu-en-est-il-français-ça-marche-alors 

  

ЧÑ‚о делаÑ‚ÑÅ’, если я не Ñ…очÑÆ’, UTF-8? 

chto-delat-esli-ya-ne-hochu-utf-8 

  

מ×” אם א×*×â„¢ ×Å“א רוצ×” UTF-8 תווים? 

מ×”-אם-א×*×â„¢-×Å“א-רוצ×”-utf-8-תווים 

  

This is an Example String. What's Going to Happen to Me? 

This_is_a_Test_String_What_s_Going_to_Ha 

*/

“PHP Destek Bölümü” sayfasına dön

Kimler çevrimiçi

Bu forumu görüntüleyen kullanıcılar: Hiç bir kayıtlı kullanıcı yok ve 0 misafir