Hoppa över navigering, gå direkt till textinnehållet



Cleaner urls in Textpattern

Some of my post are in swedish and this language includes som chars that not the english language has, like the åäö characters.

There is a problem with accented characters in textpattern when it comes to clean urls, either åäö will be stripped out or translated into something like 229 228 246 and that isnt what i would call a clean url. I rather have a solution that will translate å to a and ä to a and ö to o. There is a way to come around this problem but that would include that you type in the url you want for your self, but it is easy to forget and it would be much better if this could be done automaticly.

So did this fellow so he put together a conversion table for alot of other characters that i couldnt even think of.

With that list in mind I did a very quick function for the translation.

Get the glx_remove_accents function here note that you must have your editors encoding on UTF-8 to view the chars correct.

To have this function to work with textpattern, place it in taghandlers.php, then find the function stripSpace and edit it to look like the below function.

  1. function stripSpace($text)
  2. {
  3. global $txpac;
  4. if ($txpac['attach_titles_to_permalinks']) {
  5. $text = preg_replace("/(^| &\S+;)|(<[^>]*>)/U","",$text);
  6. if ($txpac['permalink_title_format']) {
  7. return
  8. glx_remove_accents(
  9. strtolower(
  10. preg_replace("/[^[:alnum:]\-]/","",
  11. str_replace(" ","-",
  12. $text
  13. )
  14. )
  15. )
  16. );
  17. } else {
  18. return glx_remove_accents(
  19. preg_replace("/[^[:alnum:]]/","",$text)
  20. );
  21. }
  22. }
  23. }
  24. Download this code /code/044.txt

What we do here is just that we call the glx_remove_accents function before we return the url.

Any other ideas how to manage this?

17. januari 2005, 12:55

Looks great Johan, thanks!

I will try to add this to my WordPress web sites too.

Are you OK with me using this outside of TP? Maybe the WP people will like to include it too.

Thanks again.

17. januari 2005, 13:17

No problem, of course you could use it, just glad I could help. Many thanks for the conversion table, without that one i guess only åäö would be in the list :)

17. januari 2005, 13:27

As a note, the chars in the list that should be translated into two chars is left out in this function.

Tobias Bergius sa,
26. januari 2005, 03:29

Får inte det här att fungera. :(
Har placerat innehållet ur både 043.txt och 044.txt i taghandler.php, men ändå formateras URIn med siffror.

26. januari 2005, 15:44

Hej Tobias!

Jag har bara testat detta i den senaste versionen av TXP RC2, men det kan vara så att din editor inte kan visa alla tecken korrekt och därför flippar den ur.

Testa då istället denna lite enklare funktionen med lite mindre tecken. Men som editorn antagligen klarar att visa korrekt.

045.txt

Tobias Bergius sa,
26. januari 2005, 22:02

RC2? Så vitt jag vet finns bara RC1 ute.
Men det verkar inte fungera ändå.
Spelar det någon roll var och i vilken ordning koden placeras?

26. januari 2005, 22:10

RC2 är den senaste, den får du om du kopplar upp dig med hjälp av subversion.

Leta först upp funktionen stripSpace och ersätt den med den jag har editerat här ovan, placera sedan den nya glx_remove_accents funktionen ovan eller under, det spelar ingen roll, bara det är i samma fil.

Om du inte får det att fungera så säg till så ska jag göra lite fler tester för att se vad det kan vara.

Tobias Bergius sa,
26. januari 2005, 22:25

Subversion?
Vad är skillnaderna mellan RC1 och RC2?

Den konverterar fortfarande till siffror.
Jag kör RC1.

26. januari 2005, 22:56

ops, kom precis på en sak!
Jag har ju bara testat detta på sajter där jag använder Markdown istället för textile för formateringen.

Men jag ska se om jag kan komma på en lösning så att det fungerar med textile med.

sorry.

Tobias Bergius sa,
26. januari 2005, 23:20

Hm, okej.
Fast jag tror att jag kommer slänga in Markdown också. Det ser intressant ut.

26. januari 2005, 23:51

Jag tror jag gillar Markdown mer faktiskt. Dock kör jag Textile fortfarande på denna sida, kan inte riktigt bestämma om jag ska byta här eller inte.

Men hur som helst, här är en fix som jag har testa på denna sida där det funkade.

Återställ stripSpace så som den var från början. lägg till $txpcfg till global. I första if satsen lägg till dessa rader.

include_once $txpcfg['txpath'].'/lib/classTextile.php';
$textile = new Textile();
$text = glx_remove_accents (
$textile->decode_high($text)
);

Hoppas det ska funka för dig med.

Tobias Bergius sa,
27. januari 2005, 00:25

Det fungerar lite bättre nu iaf. :)
Den konverterar allting till ‘a’—även ö.
Det är alltså den senaste glx_remove_accents jag kör med. I den första så blir det typ ylz.

27. januari 2005, 07:58

hehe, men då är det på rätt väg iaf :) jag har testat det på denna artiklen. Så det ska kunna fungera iaf :)

Men jag skickar dig ett mail med hela den funktionen som jag har så får vi hoppas att det ska lösa det :)

23. februari 2005, 14:43

Glad to see a the discussion around this, too bad I speak no sweedish :-)

Johan, there was an error in my conversion table, the L strings were converted to a K. Another error was on the line ij, which had reversed the source with target. The table is now corrected.

Would it be possible to add an extra parsing rule for the 3×2 characters that should be converted to couples?

I mean these:

Æ=Ae
?=IJ
Œ=OE
æ=ae
?=ij
œ=oe
ß=ss
?=ss

Some of these are quite popular, e.g. ß in German and Œ in French.

Emil sa,
14. mars 2005, 14:32

Tänkte testa detta men jag får samma fel som Tobias Bergius beskriver. Jag har försökt att gör som du skrev men vet inte om det blir rätt eftersom jag inte är så haj på php och textpattern än. Skulle man kunna få den färdiga funktionen??

14. mars 2005, 16:24

Emil: Det absolut enklaste sättet att få detta att fungera är att installera 1.0RC3, för då finns denna funktionalitet inbyggd från grunden.

Michelles CIU resaEn liten updatering