Skill/PHP

PHP - 문자열에서 HTML, PHP 태그 제거 : strip_tags()

2024. 5. 1.
목차
728x90
반응형

닌니디자인 PHP 썸네일

strip_tags()

주어진 문자열에서 모든 HTML 및 PHP 태그를 제거하여 순수한 텍스트 데이터만 남긴다. 보안 문제를 방지하거나 사용자 입력값을 정리하는 등의 데이터 처리 작업에 활용할 수 있다.

echo strip_tags($string, $allowed_tags);

 

 

예시

$string = "<p>This is a <b>bold</b> example.</p>";
$stripped_string = strip_tags($string);
echo $stripped_string;
This is a bold example.
check_circle Check

해당 함수는 XSS 공격을 방지하기 위해서 사용해서는 안된다. 출력 텍스트에 따라 htmlspecialchars() 또는 기타 수단과 같은 더 적절한 함수를 사용해야 한다.

 

 

 

$allowed_tags

선택적 두번째 매개 변수를 사용하여 제거해서는 안되는 태그를 지정할 수 있다. 이들은 문자열로 제공되거나 PHP 7.4.0부터 배열로 제공된다.

$string = "<p>This is a <b>bold</b> example.</p>";
$stripped_string = strip_tags($string, '<p>');
echo $stripped_string;
This is a bold example.
wb_incandescent Tip

자동 닫힘 XHTML 태그는 무시되며 자동 닫힘이 아닌 태그만 사용해야한다.

 

 

 


 

참고사이트
 

PHP: strip_tags - Manual

With most web based user input of more than a line of text, it seems I get 90% 'paste from Word'. I've developed this fn over time to try to strip all of this cruft out. A few things I do here are application specific, but if it helps you - great, if you c

www.php.net

 

728x90
반응형
카테고리 다른글 더보기