๊ฐœ๋ฐœ๋…ธํŠธ

[php] htmlspecialchars ์—๋Ÿฌ ํ•ด๊ฒฐ

๐Ÿป๊ณฐ์ด๐Ÿป 2023. 2. 10.
728x90

์—‘์…€ํŒŒ์ผ ๋‹ค์šด๋ฐ›๊ณ ๋‚˜์„œ ํŒŒ์ผ์ด ํ™•์žฅ์ž๊ฐ€ ๋ฐ”๋€Œ์—ˆ๋‹ค๊ณ  ๋‚˜์˜จ์ฆ์ƒ.

 

ํ™•์ธํ•ด๋ณด๋‹ˆ

htmlspecialchars(): charset `utf8' not supported, assuming utf-8 in

์—๋Ÿฌ๊ฐ€ ๋‚˜์™€์žˆ์—ˆ๋‹ค.

 

๋‚ด์žฅ ํ•จ์ˆ˜์ •์˜

htmlspecialchars(
    string $string,
    int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401,
    ?string $encoding = null,
    bool $double_encode = true
): string

 

charset `utf8'  ์—๋Ÿฌ๊ฐ™์€๊ฒฝ์šฐ ๋ฒˆ์—ญํ•ด๋ณด๋ฉด ๋ฌธ์ž์—ด ์…‹์ด utf8์ง€์›์„ ํ•˜์ง€์•Š๋Š”๋‹ค.

 

์ด๋Ÿด๊ฒฝ์šฐ ๋ฌธ์ž์—ด์„ ์ •์˜๋ฅผ ํ•ด์ฃผ๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

 

์ˆ˜์ • ์ „.

<?php
	htmlspecialchars($contents ?? '')
?>

 

์ˆ˜์ • ํ›„

 

<?php
	htmlspecialchars($contents, ENT_SUBSTITUTE, 'ISO-8859-1');
?>

 

์ฐธ์กฐ ์‚ฌ์ดํŠธ

https://www.php.net/manual/en/function.htmlspecialchars.php

 

PHP: htmlspecialchars - Manual

One MUST specify ENT_HTML5 in addition to double_encode=false to avoid double-encoding.The reason is that contrary to the documentation, double_encode=false will NOT unconditionally and globally prevent double-encoding of ALL existing entities. Crucially,

www.php.net

์ค‘๊ฐ„์— ๋“ค์–ด๊ฐ„ ENT_SUBSTITUTE ์˜๋ฏธํ•˜๋Š”๊ฒƒ์€

์œ ํšจํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ ๋‹จ์œ„ ์‹œํ€€์Šค๋ฅผ ์œ ๋‹ˆ์ฝ”๋“œ ๋Œ€์ฒด ๋ฌธ์ž U+FFFD(UTF-8) ๋˜๋Š” ๏ฟฝ (๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด) ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋Œ€์‹ .

 

utf8์ด๋‚˜ ๋น„์–ด์žˆ๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š”์—ญํ• ์„ ํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์„ธ๋ฒˆ์จฐ  $encoding๋ถ€๋ถ„ 

 

encoding

๋ฌธ์ž๋ฅผ ๋ณ€ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ธ์ฝ”๋”ฉ์„ ์ •์˜ํ•˜๋Š” ์„ ํƒ์  ์ธ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ƒ๋žต ํ•˜๋ฉด default_charset ๊ตฌ์„ฑ ์˜ต์…˜ encoding์˜ ๊ฐ’์ด ๊ธฐ๋ณธ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค .

์ด ์ธ์ˆ˜๋Š” ๊ธฐ์ˆ ์ ์œผ๋กœ ์„ ํƒ ์‚ฌํ•ญ์ด์ง€๋งŒ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์— ๋Œ€ํ•ด default_charset ๊ตฌ์„ฑ ์˜ต์…˜์ด ์ž˜๋ชป ์„ค์ •๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์ฝ”๋“œ์— ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค .

htmlspecialchars() ์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๋ฌธ์ž ๊ฐ€ ์ด๋Ÿฌํ•œ ๋ชจ๋“  ์ธ์ฝ”๋”ฉ์—์„œ ๋™์ผํ•œ ์œ„์น˜๋ฅผ ์ฐจ์ง€ํ•˜๋ฏ€๋กœ ์ธ์ฝ”๋”ฉ์— ๋Œ€ํ•ด ์ž์ฒด๊ฐ€ ์œ ํšจํ•œ ๊ฒฝ์šฐ ์ด ํ•จ์ˆ˜์˜ ๋ชฉ์ ์„ ์œ„ํ•ด ์ธ์ฝ”๋”ฉ ISO-8859-1, ISO-8859-15, UTF-8, cp866, cp1251, cp1252๋ฐ KOI8-R๋Š” ์‚ฌ์‹ค์ƒ ๋™์ผ ํ•ฉ๋‹ˆ๋‹ค.string

๋‹ค์Œ ๋ฌธ์ž ์ง‘ํ•ฉ์ด ์ง€์›๋ฉ๋‹ˆ๋‹ค.

 

encoding์€ 'ISO-8859-1'๋กœ ํ–ˆ๋‹ค.

 

์ˆ˜์ •ํ•˜๊ณ ๋‚˜์„œ ์ž˜ ์ž‘๋™ํ–ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€