본문 바로가기

WEBD/PHP

htmlspecialchars 와 json 의 관계

htmlspecialchars는 특수문자를 HTML 엔티티로 변환해준다.

 

수행되는 번역

Character Replacement
& (ampersand) &
" (double quote) ", unless  ENT_NOQUOTES  is set
' (single quote) ' (for  ENT_HTML401 ) or ' (for  ENT_XML1 ENT_XHTML  or  ENT_HTML5 ), but only when  ENT_QUOTES  is set
< (less than) <
> (greater than) >

사용가능한 상수

Constant Name Description
ENT_COMPAT Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES Will convert both double and single quotes.
ENT_NOQUOTES Will leave both double and single quotes unconverted.
ENT_IGNORE Silently discard invalid code unit sequences instead of returning an empty string. Using this flag is discouraged as it » may have security implications.
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#xFFFD; (otherwise) instead of returning an empty string.
ENT_DISALLOWED Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#xFFFD; (otherwise) instead of leaving them as is. This may be useful, for instance, to ensure the well-formedness of XML documents with embedded external content.
ENT_HTML401 Handle code as HTML 4.01.
ENT_XML1 Handle code as XML 1.
ENT_XHTML Handle code as XHTML.
ENT_HTML5 Handle code as HTML 5.

 그러므로 이함수를 활성화해서 사용하는경우 json으로 변환되는 값은 " 를 사용하므로 

이 값들을 무시하게 해주어야 한다.

즉 POST, GET, REQUEST 등으로 넘어오는 값에 변경해야할 문자가 존재하면 모두 변경해 버림으로

json값 자체에 번역되어 들어오는 값으로 인식이 되어 정상적인 처리가 되지 않는다.

 

해경방법은 

# htmlspecialchars($data, ENT_NOQUOTES);

와 같이 사용하여 값을 넘겨주는 방법과

# $menudata_json = htmlspecialchars_decode($this->input->post('menudata'));

이처럼 post로 넘어온 값에 대하여 역으로 풀어주는 방법이 있다.

 

 

LIST