Welcome Guest, Not a member yet? Register   Sign In
Foreign Characters url_title
#1

[eluser]Eric Barnes[/eluser]
Here is a small change I made to convert foreign characters to their English equivalent.

Create My_url_helper.php and place in application/helpers/ folder. Next add this code:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// ------------------------------------------------------------------------

/**
* Create URL Title
*
* Takes a "title" string as input and creates a
* human-friendly URL string with either a dash
* or an underscore as the word separator.
*
* @access    public
* @param    string    the string
* @param    string    the separator: dash, or underscore
* @return    string
*/
if (! function_exists('url_title'))
{
    function url_title($str, $separator = 'dash')
    {
        if ($separator == 'dash')
        {
            $search        = '_';
            $replace    = '-';
        }
        else
        {
            $search        = '-';
            $replace    = '_';
        }
        
        $str = convert_accented_characters($str);
        
        $trans = array(
                        $search                                => $replace,
                        "\s+"                                => $replace,
                        "[^a-z0-9".$replace."]"                => '',
                        $replace."+"                        => $replace,
                        $replace."$"                        => '',
                        "^".$replace                        => ''
                       );

        $str = strip_tags(strtolower($str));
    
        foreach ($trans as $key => $val)
        {
            $str = preg_replace("#".$key."#", $val, $str);
        }
    
        return trim(stripslashes($str));
    }
}

// ------------------------------------------------------------------------

/**
* Convert Special Chars
*
* Found Via:
* http://us.php.net/manual/en/function.chr.php#72145
*
* @access    public
* @param    string    the string
* @return    string
*/
function convert_accented_characters($string)
{
    for ($i=0;$i<strlen($string);$i++)
    {
        $chr = $string{$i};
        $ord = ord($chr);
        
        if ($ord>32 and $ord<126 or $ord==195)
            continue;
        elseif ($ord==161 or $ord>191 and $ord<198)
            $string{$i} = 'a';
        elseif ($ord==169 or $ord>199 and $ord<204)
            $string{$i} = 'e';
        elseif ($ord>203 and $ord<208)
            $string{$i} = 'i';
        elseif ($ord>209 and $ord<215)
            $string{$i} = 'o';
        elseif ($ord>216 and $ord<221)
            $string{$i} = 'u';
        elseif ($ord>223 and $ord<230)
            $string{$i} = 'a';
        elseif ($ord>231 and $ord<236)
            $string{$i} = 'e';
        elseif ($ord>235 and $ord<240)
            $string{$i} = 'i';
        elseif ($ord>241 and $ord<247)
            $string{$i} = 'o';
        elseif ($ord>249 and $ord<253)
            $string{$i} = 'u';
        elseif ($ord>252 and $ord<256)
            $string{$i} = 'y';
        elseif ($ord==241)
            $string{$i} = 'n';
        elseif ($ord==209)
            $string{$i} = 'N';
        else
            $string{$i} = '';
    }
    return $string;
}

?&gt;

This will hopefully convert á=a, é=e, etc. If anyone sees any problems or improvements please pass them along.
#2

[eluser]Firestorm ZERO[/eluser]
Thanks! Going to try it out.
#3

[eluser]Firestorm ZERO[/eluser]
Slight issue. It is seems to change spaces into something and when url_title comes in it will remove it.

So something like url_title('this is a test') becomes 'thisisatest' instead of 'this-is-a-test'
#4

[eluser]Firestorm ZERO[/eluser]
Found another code snippet on the web that might work...

function convert_accented_characters($string)
{
return strtr($string,
"ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ",
"SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
}
#5

[eluser]Eric Barnes[/eluser]
Oops. That last line:
Code:
else
    $string{$i} = '';

Should be:

Code:
else
    $string{$i} = ' ';
#6

[eluser]Almino Melo[/eluser]
The only one that worked for me on a win7 wampserver.
Code:
function convert_accented_characters($string) {
  return strtr($string,array(
          'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
          'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
          'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
          'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
          'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
          'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
          'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
          'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
  ));
#7

[eluser]Almino Melo[/eluser]
I made some modifications so you can chose to transform the text into lowercase or not.

And I added "space" as a separator to perform a JavaScript search (just a thing I needed to do).

Brazilian Portuguese
Com essa função você transformará todos as letras acentuadas em letras não acentuadas. Exemplo: "Não faça pão" retornaria "Nao faca pao".

Modifiquei a função para que ela permitisse escolher entre todas as letras minúsculas ou não. E adicionei a opção de separar as palavras com espaço (precisava para uma pesquisa em JavaScript).


Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// ------------------------------------------------------------------------

/**
* Create URL Title
*
* Takes a "title" string as input and creates a
* human-friendly URL string with either a dash,
* an underscore or a space as the word separator.
*
* @access  public
* @param   string  the string
* @param   string  the separator: dash, underscore or space
* @param   string  lowercase: TRUE or FALSE
* @return  string
*/
if (! function_exists('url_title')) {
  function url_title($str, $separator = 'dash', $lowercase = FALSE) {
    if ($separator == 'dash') {
      $search   = '_';
      $replace  = '-';
    }
    else if ($separator == 'underscore') {
      $search   = '-';
      $replace  = '_';
    }
    else {
      $search   = ' ';
      $replace  = ' ';
    }

    $str = convert_accented_characters($str);

    $trans = array(
            $search                     => $replace,
            "\s+"                       => $replace,
            "[^a-zA-Z0-9".$replace."]"  => '',
            $replace."+"                => $replace,
            $replace."$"                => '',
            "^".$replace                => ''
    );

    if ($lowercase) {
      $str = strtolower($str);
    }
    $str = strip_tags($str);

    foreach ($trans as $key => $val) {
      $str = preg_replace("#".$key."#", $val, $str);
    }

    return trim(stripslashes($str));
  }
}

// ------------------------------------------------------------------------

/**
* Convert Special Chars
*
* Found Via:
* http://us.php.net/manual/en/function.chr.php#72145
*
* @access    public
* @param    string    the string
* @return    string
*/
function convert_accented_characters($string) {
  return strtr($string,array(
          'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
          'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
          'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
          'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
          'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
          'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
          'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
          'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
  ));
}

?&gt;
#8

[eluser]Sbioko[/eluser]
Thanks! Small and good contribution. I'll try it later. ;-)
#9

[eluser]rip_pit[/eluser]
thank you! seems to work fine with the few tests I did Big Grin
#10

[eluser]Unknown[/eluser]
Almino Melo. Smile

I extended your code because the hungarian characters were missing from your array.

Add these lines to work with hungarian characters:

Code:
return strtr($string,array(
       [...]
       'ő' => 'o',
       'Ő' => 'O',
       'ü' => 'u',
       'Ü' => 'U',
       'ű' => 'u',
       'Ű' => 'u'));




Theme © iAndrew 2016 - Forum software by © MyBB