mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 06:48:59 +08:00
enhance Inflector helper with ascii function
This commit is contained in:
@@ -475,4 +475,19 @@ class Inflector
|
|||||||
default: return $number . 'th';
|
default: return $number . 'th';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**+
|
||||||
|
* Converts all special characters to the closest ascii character equivalent.
|
||||||
|
* @param string $string the string to be converted.
|
||||||
|
* @param array $replace the characters to be replaced by spaces.
|
||||||
|
* @return string the translated
|
||||||
|
*/
|
||||||
|
public static function ascii($string, $replace = array())
|
||||||
|
{
|
||||||
|
if (!empty($replace)) {
|
||||||
|
$string = str_replace((array)$replace, ' ', $string);
|
||||||
|
}
|
||||||
|
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
|
||||||
|
return preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $string);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,4 +132,20 @@ class InflectorTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->assertEquals('21st', Inflector::ordinalize('21'));
|
$this->assertEquals('21st', Inflector::ordinalize('21'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAscii()
|
||||||
|
{
|
||||||
|
$this->assertEquals("AAAAAAAECEEEEIIIIDNOOOOOUUUUYssaaaaaaaeceeeeiiiidnooooouuuuyy",
|
||||||
|
Inflector::ascii('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ'));
|
||||||
|
$this->assertEquals("Messd up --text-- just to stress /test/ our little clean url function--",
|
||||||
|
Inflector::ascii("Mess'd up --text-- just (to) stress /test/ ?our! `little` \\clean\\ url fun.ction!?-->"));
|
||||||
|
$this->assertEquals("Perche l erba e verde",
|
||||||
|
Inflector::ascii("Perché l'erba è verde?", "'"));
|
||||||
|
$this->assertEquals("Peux-tu m aider s il te plait",
|
||||||
|
Inflector::ascii("Peux-tu m'aider s'il te plaît?", "'"));
|
||||||
|
$this->assertEquals("Tank-efter-nu-forrn-vi-foser-dig-bort",
|
||||||
|
Inflector::slug(Inflector::ascii("Tänk efter nu – förr'n vi föser dig bort")));
|
||||||
|
$this->assertEquals("Custom delimiter example",
|
||||||
|
Inflector::ascii("Custom`delimiter*example", array('*', '`')));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user