Questions tagged [strtr]

strtr is a php function that translates characters in a string, either by using an additional "to" and "from" translation string, or a single to=>from key=>value array.

37 questions
93
votes
4 answers

When to use strtr vs str_replace?

I'm having a hard time understanding when strtr would be preferable to str_replace or vice versa. It seems that it's possible to achieve the exact same results using either function, although the order in which substrings are replaced is reversed.…
andrewtweber
  • 24,520
  • 22
  • 88
  • 110
51
votes
8 answers

PHP: Replace umlauts with closest 7-bit ASCII equivalent in an UTF-8 string

What I want to do is to remove all accents and umlauts from a string, turning "lärm" into "larm" or "andré" into "andre". What I tried to do was to utf8_decode the string and then use strtr on it, but since my source file is saved as UTF-8 file, I…
BlaM
  • 28,465
  • 32
  • 91
  • 105
16
votes
4 answers

How to use strtr() to translate multibyte/accented/diacritic characters?

Does anyone have a multibyte variant of the strtr() function? Example of desired usage: Example: $from = 'ľľščťžýáíŕďňäô'; // these chars are in UTF-8 $to = 'llsctzyairdnao'; // input - in UTF-8 $str = 'Kŕdeľ ďatľov učí koňa žrať kôru.'; $str …
Martin Ille
  • 6,747
  • 9
  • 44
  • 63
8
votes
2 answers

How do I do a strtr on UTF-8 in PHP?

I'm looking for a UTF-8 compatible strtr for PHP.
joeforker
  • 40,459
  • 37
  • 151
  • 246
7
votes
5 answers

A better way to replace emoticons in PHP?

Right now I am using this function for emoticons: function emoticons($text) { $icons = array( ':)' => 'smile', ':-)' => '
Nathan
  • 11,814
  • 11
  • 50
  • 93
5
votes
1 answer

PHP: Problem converting characters with strtr

I've got the following code from php.net: $GLOBALS['normalizeChars'] = array( 'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E',…
Ivar
  • 4,344
  • 6
  • 38
  • 53
5
votes
5 answers

Conversion of strtr php function in C#

Need to convert this php code in C# strtr($input, '+/', '-_') Does an equivalent C# function exist?
Roberto Mattea
  • 335
  • 2
  • 10
3
votes
2 answers

exchanging values of a variable, by values of an array, but under condition

I have a code that compares the output with the values of the array, and only terminates the operation with words in the array: First code(just a example) $myVar = 'essa pizza é muito gostosa, que prato de bom sabor'; $myWords=array( …
3
votes
2 answers

strtr acting weird - removing diacritics from a string

I'm having hard times removing diacritics from some $string. My code is
Martin.
  • 10,494
  • 3
  • 42
  • 68
3
votes
3 answers

strtr function not working

How are you. Hope Fine... I am facing a problem with the following code. The strtr function isn't working. It doesn't output anything... if ($translate == "DrChatrikWeb") { $convertarray = array( "~" => "~", "!" => "!", "@" => "@", "#" => "#", "$"…
Shahbaz Singh
  • 191
  • 1
  • 9
2
votes
3 answers

PHP strtr does not work at all

Even when i type echo strtr("-äåö-", "äåö", "xxx"); it does not work properly it outputs this >xxx¥x¶<, however when i use example below it does not translate nothing at all it keeps original mambo jumbo. If i type in form…
JohnA
  • 1,875
  • 8
  • 28
  • 34
2
votes
1 answer

PHP Case-insensitive equivalent of strtr

Where I could find a case-insensitive version of strtr? strtr is overloaded, I am talking about the following one string strtr ( string $str , array $replace_pairs )
fbiville
  • 8,407
  • 7
  • 51
  • 79
2
votes
1 answer

Translating with strtr in PHP fails when mixing placeholders and japanese

I'm using strtr to make translations and since I started using Japanese, it fails to replace placeholders. Here is an example string: %service% で入力してください If I just run the code below, it will not replace %service% placeholder: strtr("%service%…
ThyagoTC
  • 33
  • 6
2
votes
1 answer

How to parse a string into multiple variables based on an input mask indicating the order?

Assuming you start with this: $mask = "%name% (%user_count%) - %acyr% - %audience%"; $data = [ '%name%' => $group['name'], '%user_count%' => $group['user_count'], '%acyr%' => $group['acyr'], '%audience%' =>…
u01jmg3
  • 712
  • 1
  • 11
  • 31
2
votes
3 answers

strrchr with strtr or str_replace in PHP

I have searched a lot of sites and SO answers for replacing characters in strings, but I didn't find a solution. I need to replace only the last used character 'é' of a string. It should work for every string, also if the string only contains once…
Grischa
  • 70
  • 8
  • 26
1
2 3