Hướng dẫn dùng striphtml trong PHP

Hàm strip_tags[] là một hàm có sẵn trong ngôn ngữ PHP được dùng để tách một chuỗi khỏi HTML và các thẻ PHP.

Cú pháp:

strip_tags[ $str, $allow ];

Tham số:

  • $str: Đây là một tham số bắt buộc chỉ định chuỗi được kiểm tra.
  • $allow: Đây là một tham số tùy chọn chỉ định các thẻ được phép. Các thẻ này sẽ không bị xóa.

Ví dụ:

Kết quả:

Xin chào các bạn!

Ví dụ 2:

Kết quả:

Hello  World! 

Như vậy khi dùng hàm strip_tags[] nó đã loại bỏ các thẻ HTML ra khỏi chuỗi ký tự. Với 2 ví dụ trên hi vọng sẽ giúp bạn hiểu hơn cách thức hoạt động của hàm này.

Strip_tags trong php là một trong những từ khóa được gg search nhiều nhất về chủ đề strip_tags trong php. Trong bài viết này, cachthietkeweb.vn sẽ viết bài viết tổng hợp strip_tags trong php mới nhất 2020.

Hàm strip_tags[] sẽ loại bỏ các thẻ HTML và PHP ra khỏi chuỗi. Hàm sẽ trả về chuỗi đang loại bỏ hết các thẻ HTML và PHP.

Cú pháp

Cú phápstrip_tags[ $str, $option];

Trong đó:

  • $str là chuỗi cần loại bỏ cá thẻ HTML và PHP.
  • $option là các thẻ mà bạn muốn hàm strip_tags[] k loại bỏ. Các thẻ này sẽ được giữ lại trong chuỗi trả về.

gợi ý

Code

3

4

❮ PHP String Reference

Nội dung chính

  • Definition and Usage
  • Parameter Values
  • Technical Details
  • More Examples
  • How do you remove tag strings?
  • What is strip HTML?
  • How do you remove tags in HTML?
  • How do I strip a string in HTML?

Example

Strip the string from HTML tags:

Try it Yourself »

Definition and Usage

The strip_tags[] function strips a string from HTML, XML, and PHP tags.

Note: HTML comments are always stripped. This cannot be changed with the allow parameter.

Note: This function is binary-safe.

Syntax

Parameter Values

ParameterDescription
string Required. Specifies the string to check
allow Optional. Specifies allowable tags. These tags will not be removed

Technical Details

Return Value:PHP Version:Changelog:
Returns the stripped string
4+
As of PHP 5.3.4, this function ignores self-closing XHTML tags [like
] in allow parameter
As of PHP 5.0, this function is binary-safe.
As of PHP 4.3, HTML comments are always stripped.

More Examples

Example

Strip the string from HTML tags, but allow tags to be used:

Try it Yourself »

❮ PHP String Reference


[PHP 4, PHP 5, PHP 7, PHP 8]

strip_tagsStrip HTML and PHP tags from a string

mariusz.tarnaski at wp dot pl

13 years ago

Hi. I made a function that removes the HTML tags along with their contents:

Function:


Sample text:
$text = 'sample text with

tags
';

Result for strip_tags[$text]:
sample text with tags

Result for strip_tags_content[$text]:
text with

Result for strip_tags_content[$text, '']:
sample text with

Result for strip_tags_content[$text, '', TRUE];
text with

tags

I hope that someone is useful :]

bzplan at web dot de

9 years ago

a HTML code like this:



with
... the result is:

$str = 'color is bluesize is huge
material is wood';

notice: the words 'blue' and 'size' grow together :[
and line-breaks are still in new string $str

if you need a space between the words [and without line-break]
use my function:
... the result is:

$str = 'color is blue size is huge material is wood';

the function:



the KEY is the regex pattern: '/]*>/'
instead of strip_tags[]
... then remove control characters and multiple spaces
:]

doug at exploittheweb dot com

7 years ago

"5.3.4    strip_tags[] no longer strips self-closing XHTML tags unless the self-closing XHTML tag is also given in allowable_tags."

This is poorly worded.

The above seems to be saying that, since 5.3.4, if you don't specify "
" in allowable_tags then "
" will not be stripped... but that's not actually what they're trying to say.

What it means is, in versions prior to 5.3.4, it "strips self-closing XHTML tags unless the self-closing XHTML tag is also given in allowable_tags", and that since 5.3.4 this is no longer the case.

So what reads as "no longer strips self-closing tags [unless the self-closing XHTML tag is also given in allowable_tags]" is actually saying "no longer [strips self-closing tags unless the self-closing XHTML tag is also given in allowable_tags]".

i.e.

pre-5.3.4: strip_tags['Hello World

','
'] => 'Hello World
' // strips
because it wasn't explicitly specified in allowable_tags

5.3.4 and later: strip_tags['Hello World

','
'] => 'Hello World

' // does not strip
because PHP matches it with
in allowable_tags

Dr. Gianluigi "Zane" Zanettini

6 years ago

A word of caution. strip_tags[] can actually be used for input validation as long as you remove ANY tag. As soon as you accept a single tag [2nd parameter], you are opening up a security hole such as this:

Plus: regexing away attributes or code block is really not the right solution. For effective input validation when using strip_tags[] with even a single tag accepted, //htmlpurifier.org/ is the way to go.

stever at starburstpublishing dot com dot au

5 years ago

Since strip_tags does not remove attributes and thus creates a potential XSS security hole, here is a small function I wrote to allow only specific tags with specific attributes and strip all other tags and attributes.

If you only allow formatting tags such as b, i, and p, and styling attributes such as class, id and style, this will strip all javascript including event triggers in formatting tags.

Note that allowing anchor tags or href attributes opens another potential security hole that this solution won't protect against. You'll need more comprehensive protection if you plan to allow links in your text.

abe

1 year ago

Note, strip_tags will remove anything looking like a tag - not just tags - i.e. if you have tags in attributes then they may be removed too,

e.g.

   

Chủ Đề