Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby 1.9.x replace sets of characters with specific cleaned up characters in a string
    primarykey
    data
    text
    <p>I'm looking for a way to do the following PHP code in Ruby in a succinct and efficient manner:</p> <pre><code>$normalizeChars = array('Š'=&gt;'S', 'š'=&gt;'s', 'Ð'=&gt;'Dj','Ž'=&gt;'Z', 'ž'=&gt;'z', 'À'=&gt;'A', 'Á'=&gt;'A', 'Â'=&gt;'A', 'Ã'=&gt;'A', 'Ä'=&gt;'A', 'Å'=&gt;'A', 'Æ'=&gt;'A', 'Ç'=&gt;'C', 'È'=&gt;'E', 'É'=&gt;'E', 'Ê'=&gt;'E', 'Ë'=&gt;'E', 'Ì'=&gt;'I', 'Í'=&gt;'I', 'Î'=&gt;'I', 'Ï'=&gt;'I', 'Ñ'=&gt;'N', 'Ò'=&gt;'O', 'Ó'=&gt;'O', 'Ô'=&gt;'O', 'Õ'=&gt;'O', 'Ö'=&gt;'O', 'Ø'=&gt;'O', 'Ù'=&gt;'U', 'Ú'=&gt;'U', 'Û'=&gt;'U', 'Ü'=&gt;'U', 'Ý'=&gt;'Y', 'Þ'=&gt;'B', 'ß'=&gt;'Ss','à'=&gt;'a', 'á'=&gt;'a', 'â'=&gt;'a', 'ã'=&gt;'a', 'ä'=&gt;'a', 'å'=&gt;'a', 'æ'=&gt;'a', 'ç'=&gt;'c', 'è'=&gt;'e', 'é'=&gt;'e', 'ê'=&gt;'e', 'ë'=&gt;'e', 'ì'=&gt;'i', 'í'=&gt;'i', 'î'=&gt;'i', 'ï'=&gt;'i', 'ð'=&gt;'o', 'ñ'=&gt;'n', 'ò'=&gt;'o', 'ó'=&gt;'o', 'ô'=&gt;'o', 'õ'=&gt;'o', 'ö'=&gt;'o', 'ø'=&gt;'o', 'ù'=&gt;'u', 'ú'=&gt;'u', 'û'=&gt;'u', 'ý'=&gt;'y', 'ý'=&gt;'y', 'þ'=&gt;'b', 'ÿ'=&gt;'y', 'ƒ'=&gt;'f'); $cleanGenre = strtr($this-&gt;entryArray['genre'], $normalizeChars); </code></pre> <p>Here the <code>strtr()</code> function will replace the character on the left with the one on the right in the array. Pretty handy for a cleanup job. But I can't seem to find anywhint similar in Ruby, that is, a way to specify which characters to replace all in one array rather than with lengthy conditionals for each character.</p> <p>Note that <code>tr</code> won't work cause you can't replace one letter with two (D => Dj). Plus it gives me an <code>InvalidByteSequenceError: "\xC5" on US-ASCII</code> for this line:</p> <pre><code> entry["genre"].tr('ŠšŽž', 'SsZz') </code></pre> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload