Autor Zpráva
slovakCZ
Profil
Dobry den,
snazim se vytvorit automaticky tzv. friendy url... napr kdyz zadam: "Já jsem Lukáš" tak by to php melo prevest na "ja-jsem-lukas". Ovsem nechapu proc, ale vznikaji mi same nesmysly =o/ mam takovyto kod a dany retezec pomoci formulare predavam pres POST.
$retezecNew       = $_POST['value'];
      $retezecNew       = StrTr ($retezecNew, "ÁÄČÇĎÉĚËÍŇÓÖŘŠŤÚŮÜÝŽáäčçďéěëíňóöřšťúůüýž ", "AACCDEEEINOORSTUUUYZaaccdeeeinoorstuuuyz-");
      $retezecNew       = strtolower ($retezecNew);
      $re = "/[^[:alpha:][:digit:]]/";  
      $replacement = "-";  
      $retezecNew = preg_replace ($re, $replacement, $retezecNew);
      $retezecNew = trim ($retezecNew, "-");
      $re = "/[-]+/";  
      $replacement = "-";  
      $retezecNew = preg_replace ($re, $replacement, $retezecNew);


kdyz do formulare zadam toto:
žý hajksd ýáíěšč 213

tak php mi to prelouska na:
y?-z-hajksd-z-r-y-ru-213

nemate nekdo poneti proc?
Keeehi
Profil
slovakCZ:
A jaké kódování používáš? Pokud utf-8 nebo jemu podobné (vícebytové), tak tam by mohly být problémy např u StrTr.
slovakCZ
Profil
pouzivam UTF 8... no.. tak to je blby =o/ zmenit kodovani nemuzu, uz jsem zousel a nefungovalo zase neco jineho =o/
slovakCZ
Profil
no, asi vyuziju tento kod:
http://php.vrana.cz/vytvoreni-pratelskeho-url.php
jak tak koukam tak funguje :o)
Keeehi
Profil
slovakCZ:
http://cz.php.net/manual/en/function.strtr.php#85556
http://cz.php.net/manual/en/function.strtr.php#90925

nebo použít jinou funkci
slovakCZ
Profil
Keeehi:
aha :o) dekuji za informaci... uvidim zda se osvedci reseni z php.vrana.. jinak diky za tip, prostuduju ;)
Majkl578
Profil
$retezecNew = ' Příliš žluťoučký kůň úpěl ďábelské ódy';

$convert = array_combine(str_split('ÁÄČÇĎÉĚËÍŇÓÖŘŠŤÚŮÜÝŽáäčçďéěëíňóöřšťúůüýž', 2/* bytes*/), str_split('AACCDEEEINOORSTUUUYZaaccdeeeinoorstuuuyz'));
foreach ($convert as $from => $to) {
	$retezecNew = str_replace($from, $to, $retezecNew);
}

$retezecNew = strtolower($retezecNew);
$retezecNew = preg_replace('~[^a-z0-9]+~', '-', $retezecNew);
$retezecNew = trim($retezecNew, '-');

echo $retezecNew;//prilis-zlutoucky-kun-upel-dabelske-ody


Mnou napsané funkční řešení.
blizzboz
Profil
slovakCZ:
toto moje riešenie podporuje aj diakritiku:

public function getCoolUri($title)
{
	$uri = str_replace(array('$', '&', '+', ',', '/', ':', ';', '=', '?', '@', '<', '>', '#', '%', '{', '}', '|', '\\', '^', '~', '[', ']', '`'), '', $title);
	$uri = trim($uri);
	$uri = str_replace(' ', '-', $uri);
	return mb_strtolower($uri);
}
fuckin
Profil
Toto je mé pro odstranění diakritiky v utf 8
/** odstrani diakritiky z utf8 **/
function utf8_to_ASCII($s)
{
    static $tbl = array("\xc3\xa1" => "a","\xc3\xa4" => "a","\xc4\x8d" => "c","\xc4\x8f" => "d","\xc3\xa9" => "e","\xc4\x9b" => "e","\xc3\xad" => "i","\xc4\xbe" => "l","\xc4\xba" => "l","\xc5\x88" => "n",
        "\xc3\xb3" => "o","\xc3\xb6" => "o","\xc5\x91" => "o","\xc3\xb4" => "o","\xc5\x99" => "r","\xc5\x95" => "r","\xc5\xa1" => "s","\xc5\xa5" => "t","\xc3\xba" => "u","\xc5\xaf" => "u","\xc3\xbc" => "u","\xc5\xb1" =>
        "u","\xc3\xbd" => "y","\xc5\xbe" => "z","\xc3\x81" => "A","\xc3\x84" => "A","\xc4\x8c" => "C","\xc4\x8e" => "D","\xc3\x89" => "E","\xc4\x9a" => "E","\xc3\x8d" => "I","\xc4\xbd" => "L","\xc4\xb9" =>
        "L","\xc5\x87" => "N","\xc3\x93" => "O","\xc3\x96" => "O","\xc5\x90" => "O","\xc3\x94" => "O","\xc5\x98" => "R","\xc5\x94" => "R","\xc5\xa0" => "S","\xc5\xa4" => "T","\xc3\x9a" => "U","\xc5\xae" =>
        "U","\xc3\x9c" => "U","\xc5\xb0" => "U","\xc3\x9d" => "Y","\xc5\xbd" => "Z");
    return strtr($s,$tbl);
}

/** vytvori pratelske url **/
function friendly_url($nadpis)
{
    $url = $nadpis;
    $url = preg_replace('~[^\\pL0-9_]+~u','-',$url);
    $url = trim($url,"-");
    $url = utf8_to_ASCII($url);
    $url = strtolower($url);
    $url = preg_replace('~[^-a-z0-9_]+~','',$url);
    return $url;
}


$url = friendly_url("Ferda Mravenec Pěl Ďábelské ódy");

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm: