Share on Facebook Share on Twitter

I have a project using preg_replace. I try to search on google: how to remove all attributes from a tag using PHP? The results are complicated. Then i fixed it myself.

What is HTML Attributes

HTML attributes provide additional information about HTML elements. The HTML standard does not require lowercase attribute names. The title attribute (and all other attributes) can be written with uppercase or lowercase like title or TITLE. The HTML standard does not require quotes around attribute values. Double quotes around attribute values are the most common in HTML, but single quotes can also be used. In some situations, when the attribute value itself contains double quotes, it is necessary to use single quotes:

HTML Attributes

  • All HTML elements can have attributes
  • Attributes provide additional information about elements
  • HTML Attributes are always specified in the start tag
  • Attributes usually come in name/value pairs like: name=”value”

Remove all attributes from all html tag

The first result show me a way to remove all attributes from any html tag. It cover all html tag like: a href, p, br, table… It mean all html tag will become basic tag.

Here is function:

For example if you have html code like this:

The RegExp here mean:

When using above function it will output like this:

That’s not what i expected.  What i want is something to remove only  p tag or a tag or br tag.

PHP remove all attributes from a specific tag

After sometime searching and i discover the best way to do it using PHP.
Here is the code which i used:

Apply this code for whatever tag you want to remove attributes.
For example, i have table like this:

Then i apply above code:

Here is output after i used preg_replace:

Well, very clean, right.

Same tags:

  1. Cách ẩn các thông báo lỗi (Warnings and Notices) trên WordPress
  2. Automatically deleting cache folder using PHP & Cron Jobs
  3. Tạo hệ thống vote giống youtube
  4. Phần mềm hữu ích cho việc lập trình PHP
  5. PHP – Convert number to text
  6. PHP Random Quotes – Random Text script

Other Posts you may be interested in:

  1. Tổng hợp khuyến mại Black Friday & Cyber Monday
  2. Lịch thi đấu ngoại hạng Anh mùa giải 2015-2016
  3. Tăng tốc máy tính với CCleaner và vài thủ thuật nhỏ
  4. PHP Smart download – Hide real files path
  5. Giảm giá lớn tới 75% tại Hostgator
  6. Dropdown Menu using Jquery and CSS
  7. Chọn tuổi kết hôn – chọn chồng/vợ hợp tuổi

Last updated on July 12th, 2020 at 05:21 pm


What do you think?

This website is using cookies. More info. That's Fine