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. Adobe CC 2021 Direct Download Links
  2. Download trọn bộ Font chữ ABC, VNI và thư pháp
  3. Làm gì khi email bị đưa vào blacklist?
  4. How to restore Windows 8 – Windows 8.1 by System Restore
  5. Hướng dẫn cài đặt Font chữ (ABC, VNI, thư pháp …) cho Windows
  6. Hostgator đang âm thầm khuyến mại 75% OFF
  7. Tạo hệ thống vote giống youtube

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


What do you think?

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