String Replacement for Array

Asked

Viewed 84 times

1

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

For example, the string value is $. I want her replaced by dollar Sign. For that I created a array with the data of the replacement:

$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 locate the symbol and replace it with the corresponding value?

3 answers

0

Try this way:

<?php
$string = 'Teste simbolo $';
foreach ($arr_symbols as $key => $symbol) {
    $string = str_replace($key, $symbol, $string);
}
?>

0


You can go through the array with the foreach and check if a value is found with the function 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

  • With preg_match the result is more satisfactory for me. If $arr_symbols[$value] does not exist, PHP will return an error. For example, if the user types "@gmail", PHP will not be able to find the equivalent in the arrays.

  • 1

    @Pedrohsb I updated the answer, see if this is what you’re looking for.

  • Now yes! Thank you.

0

If you already have an array of 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["$"];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.