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. Cách xử lý khi bị kiến ba khoang đốt
  2. Tạo hệ thống vote giống youtube
  3. Những trang rút gọn link miễn phí tốt nhất
  4. Chọn tuổi kết hôn – chọn chồng/vợ hợp tuổi
  5. Làm gì khi email bị đưa vào blacklist?
  6. Hướng dẫn sửa lỗi font chữ tiếng Việt trong Microsoft Excel và Microsoft Word
  7. Những điều cần biết khi nâng cấp lên Windows 10

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

Comments

What do you think?