PHP htmlentities() 함수
예시
일부 문자를 HTML 엔터티로 변환:
<?php
$str = '<a href="https://www.w3schools.com">Go to
w3schools.com</a>';
echo htmlentities($str);
?>
위 코드의 HTML 출력은 (소스 보기):
<a href="https://www.w3schools.com">Go to w3schools.com</a>
위 코드의 브라우저 출력은 다음과 같습니다.
<a href="https://www.w3schools.com">Go to w3schools.com</a>
정의 및 사용
htmlentities() 함수는 문자를 HTML 엔티티로 변환합니다.
팁: HTML 엔티티를 다시 문자로 변환하려면 html_entity_decode() 함수를 사용하십시오.
팁: get_html_translation_table() 함수를 사용 하여 htmlentities()에서 사용하는 번역 테이블을 반환합니다.
통사론
htmlentities(string,flags,character-set,double_encode)
매개변수 값
Parameter | Description |
---|---|
string | Required. Specifies the string to convert |
flags | Optional. Specifies how to handle quotes, invalid encoding and the used document type. The available quote styles are:
Invalid encoding:
Additional flags for specifying the used doctype:
|
character-set | Optional. A string that specifies which character-set to use. Allowed values are:
Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8. |
double_encode | Optional. A boolean value that specifies whether to encode existing html entities or not.
|
기술적 세부 사항
반환 값: | 변환된 문자열을 반환합니다. 그러나 문자열 매개변수에 잘못된 인코딩이 포함된 경우 ENT_IGNORE 또는 ENT_SUBSTITUTE 플래그가 설정되지 않는 한 빈 문자열을 반환합니다. |
---|---|
PHP 버전: | 4+ |
변경 로그: | PHP 5.6 - 문자 집합
매개변수의 기본값을 기본 문자 집합의 값으로 변경했습니다(구성에서). PHP 5.4 - 문자 집합 매개변수 의 기본값 을 UTF-8로 변경했습니다. PHP 5.4 - ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 및 ENT_XHTML 추가 PHP 5.3 - ENT_IGNORE 상수 추가. PHP 5.2.3 - double_encode 매개변수를 추가했습니다. PHP 4.1 - 문자 집합 매개변수를 추가했습니다. |
더 많은 예
예시
일부 문자를 HTML 엔터티로 변환:
<?php
$str = "Albert Einstein said: 'E=MC²'";
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes
?>
위 코드의 HTML 출력은 (소스 보기):
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said:
'E=MC²'<br>
Albert Einstein said: 'E=MC²'
위 코드의 브라우저 출력은 다음과 같습니다.
Albert Einstein said: 'E=MC²'
Albert Einstein said: 'E=MC²'
Albert
Einstein said: 'E=MC²'
예시
서유럽 문자 집합을 사용하여 일부 문자를 HTML 엔터티로 변환:
<?php
$str = "My name is Øyvind Åsane. I'm Norwegian.";
echo htmlentities($str, ENT_QUOTES, "UTF-8"); // Will only convert double quotes (not single quotes), and uses the character-set Western European
?>
위 코드의 HTML 출력은 (소스 보기):
<!DOCTYPE html>
<html>
<body>
My name is Øyvind Åsane. I'm Norwegian.
</body>
</html>
위 코드의 브라우저 출력은 다음과 같습니다.
My name is Øyvind Åsane. I'm Norwegian.
❮ PHP 문자열 참조