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. Google PageSpeed ​​Insights thay đổi: sử dụng dữ liệu thực từ người dùng trình duyệt Chrome
  2. PHP Create Thumbnail – PHP thumbnail generator
  3. Web Page Password Protect
  4. Con ai?
  5. Cách tìm và loại bỏ shell trong mã nguồn
  6. Giáng sinh sớm với khuyến mại 75% OFF từ Hostgator
  7. Chung kết giải Sao Mai 2009 phong cách nhạc nhẹ

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


What do you think?