1

I am trying to convert the special chars in my array to html entity codes:

this is my helper array:

'specialChars' => [
    '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+',
    ',', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\',
    ']', '^', '_', '`', '{', '|', '}', '§', '©', '¶'
]

And this is the function:

    public static function convert($specialChars = [])
    {
        $htmlEntityArray = [];

        if(count($specialChars) == 0)
        {
            $specialChars = Config::get('constants.specialChars'); // gets the special char from the helper array
        }

        foreach ($specialChars as $key => $value)
        {
            $htmlEntityArray = array_map("htmlentities", $specialChars);
        }

        return $htmlEntityArray;
    }

But that only returns me this array, it convert some successfully and some not:

array:32 [▼
  0 => "!"
  1 => "&quot;"
  2 => "#"
  3 => "$"
  4 => "%"
  5 => "&amp;"
  6 => "'"
  7 => "("
  8 => ")"
  9 => "*"
  10 => "+"
  11 => ","
  12 => "/"
  13 => ":"
  14 => ";"
  15 => "&lt;"
  16 => "="
  17 => "&gt;"
  18 => "?"
  19 => "@"
  20 => "["
  21 => "\"
  22 => "]"
  23 => "^"
  24 => "_"
  25 => "`"
  26 => "{"
  27 => "|"
  28 => "}"
  29 => "&sect;"
  30 => "&copy;"
  31 => "&para;"
]
2
  • 1
    why not just use php's htmlentities or htmlspecialchars functions, which are made to do this Commented May 31, 2017 at 8:43
  • 1
    re-inventing the wheel i see? Commented May 31, 2017 at 8:43

3 Answers 3

4

You have to use the ENT_QUOTES and ENT_HTML5 flags.

$specialChars = [
    '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+',
    ',', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\',
    ']', '^', '_', '`', '{', '|', '}', '§', '©', '¶'
];

var_export(array_map(function ($str) { return htmlentities($str, ENT_QUOTES | ENT_HTML5); }, $specialChars));

This returns:

array (
    0 => '&excl;',
    1 => '&quot;',
    2 => '&num;',
    3 => '&dollar;',
    4 => '&percnt;',
    5 => '&amp;',
    6 => '&apos;',
    7 => '&lpar;',
    8 => '&rpar;',
    9 => '&ast;',
    10 => '&plus;',
    11 => '&comma;',
    12 => '&sol;',
    13 => '&colon;',
    14 => '&semi;',
    15 => '&lt;',
    16 => '&equals;',
    17 => '&gt;',
    18 => '&quest;',
    19 => '&commat;',
    20 => '&lbrack;',
    21 => '&bsol;',
    22 => '&rsqb;',
    23 => '&Hat;',
    24 => '&lowbar;',
    25 => '&grave;',
    26 => '&lbrace;',
    27 => '&vert;',
    28 => '&rcub;',
    29 => '&sect;',
    30 => '&copy;',
    31 => '&para;',
)
Sign up to request clarification or add additional context in comments.

2 Comments

How would I need to edit the function to make something like this avaible: Menu::convert('my name?'); And to get as an output my name&quest;
Don't use a method, just call htmlentities($str, ENT_QUOTES | ENT_HTML5) @utdev. I suppose you could use a method if this was called in lots of places though.
2

You have to use the second parameter "flag" of htmlentities like this

$htmlEntityArray = array_map(function($char) {
  return htmlentities($char, ENT_QUOTES | ENT_HTML5);
}, $specialChars);

1 Comment

ENT_HTML5 did not show all I need ENT_QUOTES aswell
0

Note: I didn't check the entity list, thus didn't notice that all characters have translations available. I'm leaving the answer though in case it can help others with a different list of characters.


From docs (emphasis mine):

all characters which have HTML character entity equivalents are translated into these entities.

See Also

In other situations you could have a better result with mb_convert_encoding() using HTML-ENTITIES as target encoding. The problem is that there isn't an obvious pattern in your entities (most of them are basic US-ASCII characters that do not have any special meaning in HTML, thus don't need to be converted to HTML entities for any of the usual reasons). So you're left two options:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.