Yii 1.1: transliterate-url

this extension is used to make the url more clean, change the blank to dash, remove the unsafe symbols such as &%$#@
6 followers

This extension is used to make the url more clean, change the blank to dash, change the un-ascii code to ascii, such as Œ to OE, À to A and so on. Remove the unsafe symbols such as &%$#@

Requirements

Yii 1.0 or above

Usage

Download the code, and locate it in protected/components

test in your controller,

echo UrlTransliterate::cleanString("我爱 中文!");

Resources

Total 5 comments

#6660 report it
Rajcsányi Zoltán at 2012/01/26 03:51am
This extension is very good for multilingual sites

I used this code before: (/protected/components/UrlGenerator.php)

class UrlGenerator {
 
  /**
   * create url friendly text from free text
   * @param string $str free text
   * @return string url friendly text
   */
  public static function encodeStringToURL($str) {          
 
    $search  = array('ö','ó','ü','ő','ú','é','á','ű','í',
                     'Ö','Ü','Ó','Ő','Ú','É','Á','Ű','Í',
                     ' ', 'ß', 'ä', 'Ä', '.');
 
    $replace = array('o','o','u','o','u','e','a','u','i',
                     'O','U','O','O','U','E','A','U','I',
                     '_', 'ss', 'a', 'Á', '');        
 
    $str = str_replace($search, $replace, $str);
 
    $forbidden_chars = '/(?!<.*?)(%s)(?![^<>]*?>)/i';
 
    $str = preg_replace($forbidden_chars, '', $str);
 
    return $str;      
  }
 
}

The code only handle some languages from free text to URL safe text

Obviously this extension much more better. Thanks

#5577 report it
Roman Solomatin at 2011/10/21 11:16am
Thanks

My clients have multilingual websites so this comes in very handy.

#2011 report it
Davidhhuan at 2010/11/01 08:58am
Feedback

@g3ck0 Thank you for your suggestion. I have upgraded the code except the point 1: $ignoreWords. Because i think that that if the user ignore some words include the un-ASCII characters, then this class will lost its function, right? Just feel free to have a discussion. :)

@samdark Thank you for your testing, but i didn't find the error, here is the test code. (Please forgive me the words i just copy from the yii forum, and i don't know what does that mean :p )

echo UrlTransliterate::cleanString(
            'Я вот капчу определил в контроллере, вывел где нужно. Но все примеры основаны на валидаторе форм, а я его не использую... Подскажите где взять значение капчи чтобы самому в контроллере сделать проверку.'
        );

Here is the result: ya-vot-kapchu-opredelil-v-kontrollere-vyvel-gde-nuzhno-no-vse-primery-osnovany-na-validatore-form-a-ya-ego-ne-ispolzuyu-podskazhite-gde-vzyat-znachenie-kapchi-chtoby-samomu-v-kontrollere-sdelat-proverku

#2010 report it
samdark at 2010/10/31 11:31am
Feedback

Nice one. Why Russian (Cyrillic) letters are translated to uppercased English letters? Should be lowercased.

#2008 report it
g3ck0 at 2010/10/30 08:15am
Feedback

Hey,

nice class :) I have written such a component too but not that complete. Here some ideas:

  • $ignoreWords: Perhaps the user of the component wants to modify this, especially because of other languages. And perhaps it would be better that there is no default value.

  • Modify your punctuationChars() method like this:

$punctuation = array(
    'double_quotes' => '"',
    'quotes' => "'",
    'backtick' => '`',
    [...]
   );

So you have to adjust your loop in cleanString().

  • Rename $noCase in cleanSeparators() to $toLower, so its clearer what you mean.

Leave a comment

Please to leave your comment.

Create extension