'I want to use codeigniter foreign character library in my custom PHP project how i can use it?
this is array of foreign characters how I can use it in my own project.
$foreign_characters = array(
'/ä|æ|ǽ/' => 'ae',
'/ö|œ/' => 'oe',
'/ü/' => 'ue',
'/Ä/' => 'Ae',
'/Ü/' => 'Ue',
'/Ö/' => 'Oe',
'/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ|Α|Ά|Ả|Ạ|Ầ|Ẫ|Ẩ|Ậ|Ằ|Ắ|Ẵ|Ẳ|Ặ|А/' => 'A',
'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|α|ά|ả|ạ|ầ|ấ|ẫ|ẩ|ậ|ằ|ắ|ẵ|ẳ|ặ|а/' => 'a',
);
Solution 1:[1]
Well, your array already has the perfect format to use with preg_replace()
.
Substitution can easily be done by looping over your array and calling preg_replace()
once for every set of chars like this:
foreach( $foreign_characters as $replace => $with )
{
$string = preg_replace($replace, $with, $string);
}
Thats basically all you need.
To make things more convenient and easy to use, have a look at this replacement class:
class Replacer
{
/**
* List of character replacements
*/
static $foreign_characters = array(
'/ä|æ|?/' => 'ae',
'/ö|œ/' => 'oe',
'/ü/' => 'ue',
'/Ä/' => 'Ae',
'/Ü/' => 'Ue',
'/Ö/' => 'Oe',
'/À|Á|Â|Ã|Ä|Å|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?/' => 'A',
'/à|á|â|ã|å|?|?|?|?|?|ª|?|?|?|?|?|?|?|?|?|?|?|?|?|?|?/' => 'a',
);
/**
* Replaces all foreign characters listed in
* self::$foreign_characters with their given counterparts
* @param $string string to replace characters in
*/
public static function replace_chars_in($string)
{
foreach( self::$foreign_characters as $replace => $with )
{
$string = preg_replace($replace, $with, $string);
}
return $string;
}
}
I decided to create a class, because it provides an easy way to handle your replacements, and you can improve the functionality without any effort. I used a static function, so you do not need to instantiate it, the replacement would simply be done by calling Replacer::replace_chars_in();
. Try this:
echo Replacer::replace_chars_in("äÄfäüädasÖä?asd");
it will output: aeAefaeueaedasOeaeaeasd
If you are unfamiliar with regular expressions, have a read of the PHP reference. There you can find a detailed explanation on how they work.
This post on character replacement and the strtr function might interest you as well.
Solution 2:[2]
Use convert_accented_characters() function in Text Helper. https://codeigniter.com/userguide3/helpers/text_helper.html
function convert_accented_characters($str)
{
static $array_from, $array_to;
if ( ! is_array($array_from))
{
if (file_exists(APPPATH.'config/foreign_chars.php'))
{
include(APPPATH.'config/foreign_chars.php');
}
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
}
if (empty($foreign_characters) OR ! is_array($foreign_characters))
{
$array_from = array();
$array_to = array();
return $str;
}
$array_from = array_keys($foreign_characters);
$array_to = array_values($foreign_characters);
}
return preg_replace($array_from, $array_to, $str);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | Tùng Phan |