James
James

Reputation: 43617

Regex to remove id attributes from HTML

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?

Upvotes: 6

Views: 9867

Answers (4)

Murdej Ukrutn&#253;
Murdej Ukrutn&#253;

Reputation: 339

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

it eliminate also id=foo (without ") in tag and keep id= out of tag

Upvotes: 0

Till Helge
Till Helge

Reputation: 9311

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.

Upvotes: 16

Kent
Kent

Reputation: 195029

try the following:

'id="[^"]*"'

Upvotes: 5

Nahydrin
Nahydrin

Reputation: 13507

$data = preg_replace('#<id=".*?"/>#', '', $data); 

Upvotes: 0

Related Questions