String Replacement by Array

1

I want to make a string already defined be replaced by an array .

For example, the value of the string is $ . I want it to be replaced by dollar sign . For this I created an array with the replacement data:

$arr_symbols = array("’" => "apostrophe", "" => "apostrophe", "(" => "parentheses", ")" => "parentheses", "[" => "square brackets", "]" => "square brackets", "{" => "curly brackets", "}" => "curly brackets", ":" => "colon", "ː" => "ipa triangular colon", "," => "comma", "،" => "arabic comma", "–" => "en dash", "—" => "em dash", "‒" => "figure dash", "…" => "ellipsis", ". . ." => "ellipsis", "⋯" => "mid-line ellipsis", "!" => "exclamation mark", "¡" => "inverted exclamation mark", "՜" => "armenian exclamation mark", "ǃ" => "alveolar click sign", "." => "period", "-" => "hyphen-minus", "‑" => "non-breaking hyphen", "?" => "question mark", "“" => "quotation marks", "”" => "quotation marks", "’" => "quotation marks", "‘" => "quotation marks", "”" => "citation marks", "”" => "citation marks", "«" => "guillemets", "»" => "guillemets", "「" => "cjk brackets", "」" => "cjk brackets", ";" => "semicolon", "/" => "slash", "⁄" => "fraction slash", "∕" => "division slash", "•" => "interpunct", "&" => "ampersand", "*" => "asterisk", "\" => "backslash", "•" => "bullet", "◦" => "white bullet", "‣" => "triangular bullet", "^" => "circumflex aceent", "‸" => "caret", "⁁" => "caret insertion point", "^" => "fullwidth circumflex accent", "†" => "dagger", "‡" => "double dagger", "°" => "degree", "″" => "ditto mark", "¿" => "inverted question mark", '#' => "number sign", "№" => "numero sign", "÷" => "obelus", "º" => "ordinal indicator", "ª" => "ordinal indicator", "%" => "percent", "‰" => "per mil", "+" => "plus", "−" => "minus", "‱" => "per ten thousand sign", "¶" => "pilcrow", "′" => "prime", "″" => "double prime", "‴" => "triple prime", "§" => "section sign", "~" => "tilde", "˜" => "tilde", "∼" => "tilde operator", "_" => "underscore", "|" => "vertical bar", "¦" => "broken bar", "‖" => "magnitude", "∣" => "divides", "©" => "copyright", "℗" => "sound-recording copyright", "®" => "registered trademark", "⁂" => "asterism", "❧" => "fleuron", "☞" => "index", "‽" => "interrobang", "◊" => "lozenge", "※" => "reference mark", "⁀" => "tie", "$" = "dollar sign");

What is the fastest and most efficient way to find the symbol and replace it with the corresponding value?

    
asked by anonymous 25.04.2015 / 04:23

3 answers

0

You can scroll through the array with foreach and check if a value is found with the strpos :

$simbolos = array("’" => "apostrophe", "" => "apostrophe", "(" => "parentheses", ")" => "parentheses", "[" => "square brackets", "]" => "square brackets", "{" => "curly brackets", "}" => "curly brackets", ":" => "colon", "ː" => "ipa triangular colon", "," => "comma", "،" => "arabic comma", "–" => "en dash", "—" => "em dash", "‒" => "figure dash", "…" => "ellipsis", ". . ." => "ellipsis", "⋯" => "mid-line ellipsis", "!" => "exclamation mark", "¡" => "inverted exclamation mark", "՜" => "armenian exclamation mark", "ǃ" => "alveolar click sign", "." => "period", "-" => "hyphen-minus", "‑" => "non-breaking hyphen", "?" => "question mark", "“" => "quotation marks", "”" => "quotation marks", "’" => "quotation marks", "‘" => "quotation marks", "”" => "citation marks", "”" => "citation marks", "«" => "guillemets", "»" => "guillemets", "「" => "cjk brackets", "」" => "cjk brackets", ";" => "semicolon", "/" => "slash", "⁄" => "fraction slash", "∕" => "division slash", "•" => "interpunct", "&" => "ampersand", "*" => "asterisk", "\" => "backslash", "•" => "bullet", "◦" => "white bullet", "‣" => "triangular bullet", "^" => "circumflex aceent", "‸" => "caret", "⁁" => "caret insertion point", "^" => "fullwidth circumflex accent", "†" => "dagger", "‡" => "double dagger", "°" => "degree", "″" => "ditto mark", "¿" => "inverted question mark", '#' => "number sign", "№" => "numero sign", "÷" => "obelus", "º" => "ordinal indicator", "ª" => "ordinal indicator", "%" => "percent", "‰" => "per mil", "+" => "plus", "−" => "minus", "‱" => "per ten thousand sign", "¶" => "pilcrow", "′" => "prime", "″" => "double prime", "‴" => "triple prime", "§" => "section sign", "~" => "tilde", "˜" => "tilde", "∼" => "tilde operator", "_" => "underscore", "|" => "vertical bar", "¦" => "broken bar", "‖" => "magnitude", "∣" => "divides", "©" => "copyright", "℗" => "sound-recording copyright", "®" => "registered trademark", "⁂" => "asterism", "❧" => "fleuron", "☞" => "index", "‽" => "interrobang", "◊" => "lozenge", "※" => "reference mark", "⁀" => "tie", "$" => "dollar sign");
$simbolo = "$";

foreach($simbolos as $chave => $valor) {
    if (strpos($chave, $simbolo) === 0) {
        $simbolo = $valor;
    } else {
        // Fazer algo aqui caso não encontre
    }
}

echo $simbolo . "\n";

DEMO

    
25.04.2015 / 04:56
0

Try this:

<?php
$string = 'Teste simbolo $';
foreach ($arr_symbols as $key => $symbol) {
    $string = str_replace($key, $symbol, $string);
}
?>
    
25.04.2015 / 04:53
0

If you already have an array with values, the easiest thing to solve in a row is to use the symbol itself as a key. Ex.:

$arr = array("$"=>"dollar symbol", "."=>"dot", ","=>"comma");
$simbolo = "$";

Then use:

echo $arr[$simbolo];

Or

echo $arr["$"];
    
25.04.2015 / 07:20