Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Kodowanie tytułu do adresu
-lukasamd-
post 6.02.2009, 17:07:02
Post #1





Goście







Witam,
poszukuję funkcji w php, która zakoduje mi dany tekst na coś, co może znajdować się w adresie internetowym np.

"To jest przykładowy tytuł artykułu?!"

na

"to-jest-przykladowy-tytul-artykulu"

Dodam tylko, że urlencode i rawurlencode nie zamieniają wpisów na takie, na jakich mi zależy.
Chodzi mi raczej o coś takiego:

  1. <?php
  2. function decode_url($adres){
  3.   $adres = preg_replace('# <(.*?)>(.*?) <(.*?)>#si', '2', $adres);
  4.   $adres = html_entity_decode ($adres);
  5.   $adres = preg_replace ("' <[/!]*?[^ <>]*?>'si", "", $adres);
  6.   $adres = str_replace( "ˇ", "A", $adres);
  7.   $adres = str_replace( "ą", "a", $adres);
  8.   $adres = str_replace( "ł", "l", $adres);
  9.   $adres = str_replace( "Ł", "L", $adres);
  10.   $adres = str_replace( "ó", "o", $adres);
  11.   $adres = str_replace( "Ó", "O", $adres);
  12.   $adres = str_replace( "ń", "n", $adres);
  13.   $adres = str_replace( "Ń", "N", $adres);
  14.   $adres = str_replace( "ć", "c", $adres);
  15.   $adres = str_replace( "Ć", "C", $adres);
  16.   $adres = str_replace( "Ż", "Z", $adres);
  17.   $adres = str_replace( "Ź", "Z", $adres);
  18.   $adres = str_replace( "ż", "z", $adres);
  19.   $adres = str_replace( "Ľ", "z", $adres);
  20.   $adres = str_replace( "ę", "e", $adres);
  21.   $adres = str_replace( "Ę", "E", $adres);
  22.   $adres = str_replace( "Ś", "S", $adres);
  23.   $adres = str_replace( "ś", "s", $adres);
  24.   $adres = str_replace( "s64;", "EUR", $adres);
  25.   $adres = str_replace( "s64;", "EUR", $adres);
  26.   $adres = str_replace(" / ","-",$adres);
  27.   $adres = str_replace("/","-",$adres);
  28.   $adres = str_replace("'","-",$adres);
  29.   $adres = str_replace(" - ","-",$adres);
  30.   $adres = str_replace("_","-",$adres);
  31.   $adres = str_replace(" ","-",$adres);
  32.   $adres = str_replace( "%", "Procent", $adres);
  33.   $adres = ereg_replace("[^A-Za-z0-9-]", "", $adres);
  34.   $adres = str_replace("----","-",$adres);
  35.   $adres = str_replace("---","-",$adres);
  36.   $adres = str_replace("--","-",$adres);
  37.   $adres = strtolower($adres);
  38. return ($adres);
  39. }


Ale jeżeli takie coś jest już w php, to po co 2x wynajdywać koło...
Go to the top of the page
+Quote Post
Apocalyptiq
post 6.02.2009, 17:16:59
Post #2





Grupa: Zarejestrowani
Postów: 230
Pomógł: 3
Dołączył: 8.01.2008

Ostrzeżenie: (10%)
X----


Gotowej funkcji w php która zmieniałaby polskie znaki na ich odpowiedniki bez ogonków itp. pewnie nie ma, ale można nieco uprościć to replacowanie:
Kod
<?php
function decode_url($adres){
   $adres = preg_replace('# <(.*?)>(.*?) <(.*?)>#si', '\2', $adres);
   $adres = html_entity_decode ($adres);
   $adres = preg_replace ("' <[\/\!]*?[^ <>]*?>'si", "", $adres);
   $replaces=array("ˇ"=> "A", "ą"=>"a", "ł"=>"l"......);
   foreach($replaces as $from => $to) $adres = str_replace($from,$to,$adres);
   $adres = ereg_replace("[^A-Za-z0-9-]", "", $adres);
   $adres = str_replace("----","-",$adres);
   $adres = str_replace("---","-",$adres);
   $adres = str_replace("--","-",$adres);
   $adres = strtolower($adres);
return ($adres);
}

I co do dużych polskich liter, np. duże Ą od razu możesz zmieniać na małe ą, jeżeli chcesz mieć tylko małe litery ;-)

Ten post edytował Apocalyptiq 6.02.2009, 17:19:05


--------------------
http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
Go to the top of the page
+Quote Post
mls
post 7.02.2009, 03:37:15
Post #3





Grupa: Zarejestrowani
Postów: 677
Pomógł: 89
Dołączył: 31.08.2003
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


  1. <?php
  2. function encode_url ($url)
  3. {
  4.    $url = strip_tags(html_entity_decode($url));
  5.    $url = strtolower(str_replace("'", '', iconv('utf-8', 'ascii//translit', $url)));
  6.    $url = preg_replace('/[^a-z0-9-_.]/', '-', $url);
  7.    $url = preg_replace('/-+/', '-', $url);
  8.    $url = trim($url, '-');
  9.    return $url;
  10. }
  11. ?>


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 03:19