我想要的特殊字符轉換成我的數組爲HTML實體代碼:PHP特殊字符的HTML實體代碼
這是幫助我的數組:
'specialChars' => [
'!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+',
',', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\',
']', '^', '_', '`', '{', '|', '}', '§', '©', '¶'
]
這是函數:
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;
}
但是,只有返回我這陣,它成功地將一些,有些不是:
array:32 [▼
0 => "!"
1 => """
2 => "#"
3 => "$"
4 => "%"
5 => "&"
6 => "'"
7 => "("
8 => ")"
9 => "*"
10 => "+"
11 => ","
12 => "/"
13 => ":"
14 => ";"
15 => "<"
16 => "="
17 => ">"
18 => "?"
19 => "@"
20 => "["
21 => "\"
22 => "]"
23 => "^"
24 => "_"
25 => "`"
26 => "{"
27 => "|"
28 => "}"
29 => "§"
30 => "©"
31 => "¶"
]
爲什麼不使用php的['htmlentities'](http://php.net/manual/en/function.htmlentities.php)或['htmlspecialchars'](http://php.net/manual /en/function.htmlspecialchars.php)功能,這是做到這一點 – Jelmergu
重新發明我看到的車輪? – Akintunde007