Dung Quang
Dung Quang

Reputation: 363

How to replace style in html with regular expressions?

I have string html like this:

<table style="width: 630px;"></table>

and i need change it to

<table style="width: 100%;"></table>

I also try regular expression but i cannot change it. Can someone help me?

Upvotes: 0

Views: 117

Answers (1)

Joe
Joe

Reputation: 15802

If you just want to replace 630px then you can do str_replace('630px', '100%', $string);

If you need to match any px, you can do preg_replace('/[0-9]+px/', '100%', $string);

If you want to only match it where it's associated with the width, you'll want preg_replace('/width\: *[0-9]+px/', 'width: 100%', $string);

For anything more serious (only matching in the table tag, etc) you'll probably want to look into HTML parsing.

Upvotes: 1

Related Questions