2

I have my class

public function convert( $title )
    {
        $nameout = strtolower( $title );
        $nameout = str_replace(' ', '-', $nameout );
        $nameout = str_replace('.', '', $nameout);
        $nameout = str_replace('æ', 'ae', $nameout);
        $nameout = str_replace('ø', 'oe', $nameout);
        $nameout = str_replace('å', 'aa', $nameout);
        $nameout = str_replace('(', '', $nameout);
        $nameout = str_replace(')', '', $nameout);
        $nameout = preg_replace("[^a-z0-9-]", "", $nameout);    

        return $nameout;
    }

BUt I can't get it to work when I use special characters like ö and ü and other, can sombody help me here? I use PHP 5.3.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
ParisNakitaKejser
  • 12,112
  • 9
  • 46
  • 66
  • 1
    Why do you need to remove diacritics exactly? If it's just to have it pass inside an HTTP URL, you can use `urlencode`. – zneak Mar 15 '10 at 17:04

4 Answers4

3

The first answer in this SO thread contains the code you need to do this.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
2

And what about:

<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>

From: http://php.net/manual/en/function.urlencode.php

pvledoux
  • 973
  • 1
  • 10
  • 23
1

I wrote this function a while ago for a project I was working on and couldn't get RegEx to work. Its not the best way, but it works.

function safeURL($input){
    $input = strtolower($input);
    for($i = 0; $i < strlen($input); $i++){
        $working = ord(substr($input,$i,1));
        if(($working>=97)&&($working<=122)){
            //a-z
            $out = $out . chr($working);
        } elseif(($working>=48)&&($working<=57)){
            //0-9
            $out = $out . chr($working);
        } elseif($working==46){
            //.
            $out = $out . chr($working);
        } elseif($working==45){
            //-
            $out = $out . chr($working);
        }
    }
    return $out;
}
Jamescun
  • 673
  • 5
  • 8
0

Here's a function to help with what you're doing, it's written in Czech: http://php.vrana.cz/vytvoreni-pratelskeho-url.php (and translated to English)

Here's another take on it (from the Symfony documentation):

<?php 
function slugify($text)
{
  // replace non letter or digits by -
  $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

  // trim
  $text = trim($text, '-');

  // transliterate
  if (function_exists('iconv'))
  {
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  }

  // lowercase
  $text = strtolower($text);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text);

  if (empty($text))
  {
    return 'n-a';
  }

  return $text;
}
camomileCase
  • 1,706
  • 2
  • 18
  • 27