2009년 2월 12일 목요일

htmlspecialchars -> htmlspecialchars_decode 사용

htmlspecialchars로 변경된 데이터를 encode / decode하는 경우
php version 5 이상에서는
htmlspecialchars_decode 가 기본 function이지만 없는 경우 만들어 써야 함.

function htmlspecialchars_decode($string, $quote_style = null)
{
// Sanity check
if (!is_scalar($string)) {
user_error('htmlspecialchars_decode() expects parameter 1 to be string, ' .
gettype($string) . ' given', E_USER_WARNING);
return;
}

if (!is_int($quote_style) && $quote_style !== null) {
user_error('htmlspecialchars_decode() expects parameter 2 to be integer, ' .
gettype($quote_style) . ' given', E_USER_WARNING);
return;
}

// Init
$from = array('&', '<', '>');
$to = array('&', '<', '>');

// The function does not behave as documented
// This matches the actual behaviour of the function
if ($quote_style & ENT_COMPAT || $quote_style & ENT_QUOTES) {
$from[] = '&quot;';
$to[] = '"';

$from[] = '&#039;';
$to[] = "'";
}

return str_replace($from, $to, $string);
}

댓글 없음:

댓글 쓰기