Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove id attributes from HTML

Tags:

html

regex

php

I have an html with a lot of id="something" attributes.

All the html is inside $data var.

Trying to remove all the id="*" from $data:

$data = preg_replace('\<id="[*]"^\>', '', $data);

Doesn't work, whats wrong?

like image 683
James Avatar asked Oct 25 '11 12:10

James


2 Answers

Try this instead:

$data = preg_replace('#\s(id|class)="[^"]+"#', '', $data);

Note: We solved the remaining issues in chat. The answer still fits the problem described in the question.

like image 82
Till Helge Avatar answered Nov 14 '22 13:11

Till Helge


try the following:

'id="[^"]*"'
like image 42
Kent Avatar answered Nov 14 '22 15:11

Kent