Welcome Guest, Not a member yet? Register   Sign In
url_title() for other languages
#1

[eluser]dianikol85[/eluser]
Hi to all.

I want to use the url_title() helper to create some pretty urls. But i'm from Greece and with greek letters the helper doesn't work.

Any suggestions??
#2

[eluser]osci[/eluser]
in system/helpers/url_helper.php
Code:
$trans = array(
   '&\#\d+?;'        => '',
   '&\S+?;'        => '',
   '\s+'        => $replace,
   '[^a-z0-9\-\._]'    => '',
   $replace.'+'        => $replace,
   $replace.'$'        => $replace,
   '^'.$replace        => $replace,
   '\.+$'        => ''
);

this line
Code:
'[^a-z0-9\-\._]'    => '',
is giving you problems.

I am from Greece too. And I'm serving greek in my urls in the format /news/Είδηση
but not using this function.
And I write my pretty urls as a field slugs in my db and serve it from there.
to create that slug I do
Code:
function string_to_slug($str)
{
$str = str_replace(array('"', "'","!","?","@","#","$","%","^","*",";","`","~",), '', $str);
$str = str_replace(array("/", ",","."," ","<",">","(",")","[","]","{","}","&","+","-","="), '-', $str);
    return $str;
}
with the '-' in str_replace is to make spaces. You could use + if wanted. I don't know if this brings any other server or security issues. I'm using - by default.

And in order to allow codeigniter parse greek characters you have to replace in config.php
Code:
// Support for greek slug
// ** original ** $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'
             .'αάβγδεέξηήθιίϊκλμνξοόπρσςτυύϋφχψωώ'
             .'ΑΆΒΓΔΕΈΖΗΉΘΙΊΪΚΛΜΝΞΟΌΠΡΣΤΥΎΫΦΧΨΩΏ';
#3

[eluser]dianikol85[/eluser]
So there is no way by using the url_title(), eh?

Basically i also use a field in the db to use it as a slug. But i am using ajax in the form to generate the slug.

For example:

Code:
&lt;input type="text" name="title" id="title"&gt;
&lt;input type="text" name="url_title" id="url_title"&gt;


When the user types something in the "title" then with ajax i return the slug from a method using the url_title() helper. I don't want to lose this functionality

Should i write my own helper to do that? It's a same because CI have a lot of helpers like the url_title() and i want to use a lot of them
#4

[eluser]osci[/eluser]
As I showed you the helper doesn't allow greek characters and has no option for inserting custom $trans_array.
So either you are extending this helper by adding this functionality or write a separate helper for that.

It's not CI to blame. We have quite a different character set. And while in other countries it's ok for us it's not. This comes from old dos where we had our characters in the upper 127 bits while most others had it in the low bits. And unfortunately still comes. Fully implemented unicode php is our savor in this.

As to your problem If you're already using url_title() for other languages, you should extend it to support greek too.
#5

[eluser]dianikol85[/eluser]
Look this . if i comment out this line

Code:
'[^a-z0-9\-\._]'    => '',

it accepts greek. I guess it's not secure that way. What do you think?
#6

[eluser]osci[/eluser]
no the greek chars need to be added in there, don't comment that code.

If I have time later I'll make a wiki for that

I believe something like this will do the trick.
Code:
'[^a-z0-9αάβγδεέξηήθιίϊκλμνξοόπρσςτυύϋφχψωώΑΆΒΓΔΕΈΖΗΉΘΙΊΪΚΛΜΝΞΟΌΠΡΣΤΥΎΫΦΧΨΩΏ\-\._]'    => '',

But it should be better placed in a MY_Url_helper to override the current function.
Since it's of interest to me I'll check it and post a wiki about it.
#7

[eluser]dianikol85[/eluser]
cool!! post it in this thread if you can.

thanks
#8

[eluser]osci[/eluser]
I won't post in wiki since I encountered issues.
I can allow more characters but if I want lowercase I get into trouble.
strtolower breaks greek.
I used mb_strtolower and it converts greek just fine but I'm missing the english characters :/

/application/helpers/MY_url_helper.php
Code:
if (function_exists('mb_strtolower'))
{
   $str = mb_strtolower($str,'UTF-8');
} else {
   return $str;
}

As I said strtolower breaks greek so I couldn't use
Code:
$str = mb_strtolower(strtolower($str),'UTF-8');

So I'll post here the extended url helper but you can't have TRUE as a param for lowercase. It's not worth to go in wiki for greek since it's not fully functional.

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('url_title'))
{
   function url_title($str, $separator = 'dash', $lowercase = FALSE, $extended_chars='')
   {
      if ($separator == 'dash')
      {
         $search        = '_';
         $replace    = '-';
      } else {
         $search        = '-';
         $replace    = '_';
      }

      $trans = array(
        '&\#\d+?;'                => '',
        '&\S+?;'                => '',
        '\s+'                    => $replace,
        '[^a-z0-9'.$extended_chars.'\-\._]'    => '',
        $replace.'+'            => $replace,
        $replace.'$'            => $replace,
        '^'.$replace            => $replace,
        '\.+$'                    => ''
     );

     $str = strip_tags($str);

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

      if ($lowercase === TRUE)
      {
         if (function_exists('mb_strtolower'))
         {
            $str = mb_strtolower($str, 'UTF-8');
         } else {
            $str = strtolower($str);
         }
      }

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

And you call it like
Code:
url_title($str, '', FALSE, 'αάβγδεέξηήθιίϊκλμνξοόπρσςτυύϋφχψωώΑΆΒΓΔΕΈΖΗΉΘΙΊΪΚΛΜΝΞΟΌΠΡΣΤΥΎΫΦΧΨΩΏ');
#9

[eluser]dianikol85[/eluser]
Well i tried mb_strtolower() too i and i don't see any wrong data.

i tried :

THIS IS A TEST ΑΥΤΟ ΕΙΝΑΙ ΕΝΑ ΤΕΣΤ

and it returned

this-is-a-test-αυτο-ειναι-ενα-τεστ

i think it looks ok.

I think it's ok about the uppercase letters. Not a big issue i guess. And if i really need all the letters to be lowercase i can do it manually. Again i don't think it as an issue. Probably only the first letter would be uppercase most of the times. So not big deal
#10

[eluser]osci[/eluser]
Yes, you are right.
I was tired and after trying with strreplace, strtr and such, I started checking multibyte functions but in my welcome controller I had
Code:
$this->load->helper('url');
$str='ερΔδδετ4΄2 445 δε@ %2φ Υϋρεςφ 4+ %#1 ΄΄;';
$data['str']='ερΔδδετ4΄2 445 δεAsREDWE@ %2 rEAD englSH vφ Υϋρεςφ 4+ %#1 ΄΄;';
$data['urlstr']=url_title($str,'dash',TRUE,'αάβγδεέξηήθιίϊκλμνξοόπρσςτυύϋφχψωώΑΆΒΓΔΕΈΖΗΉΘΙΊΪΚΛΜΝΞΟΌΠΡΣΤΥΎΫΦΧΨΩΏ');
$this->load->view('welcome_message',$data);

notice $data['str']!!!!
I was bored looking at debugger so I started passing the var to view to check. And in my hurry I copied the string instead of passing the var!!! And I was really trying to see why I don't see english!!!

Well It works for lowercase just fine !!! and I update my previous post with the mb function.
Of course If you don't have mb strings installed you'll fall back to regular strtolower which breaks greek.

Thanks for checking. I wouldn't look back at this for some time.




Theme © iAndrew 2016 - Forum software by © MyBB