スペース、一重/二重引用符、改行などのすべてを変換したい.
入力例を次に示します ( som_nangiaに感謝します):
Escape Check < "escape these" > <“and these”> <html><tr><td></td></tr></html> 'these will need escaping too' ‘ so will these’ <script> </script>
私が検討しているオプションは次のとおりです。
<pre>Escape Check < "escape these" > <“and these”> <html><tr><td></td></tr></html> 'these will need escaping too' ‘ so will these’ <script> </script></pre>
/**
* Encoding html special characters, including nl2br
* @param string $original
* @return string
*/
function encode_html_sp_chars($original) {
$table = get_html_translation_table(HTML_ENTITIES);
$table[' '] = ' ';
$encoded = strtr($original, $table);
return nl2br($encoded);
}
htmlspecialcharsとhtmlentitiesの両方を試しましたが、スペースをエンコードするものはありません。