0

Not sure why the code below isn't working. Any ideas?

<table width="567"

$text = preg_replace('/<table width=["\']\s*\d+\s*(px|%)\s*["\']/', '<table width="100%"', $text);

Alex333
  • 217
  • 1
  • 5
  • 1
    Don't use RegEx to parse HTML. Use a proper parser instead. – Cid Jan 08 '20 at 13:32
  • I see you're using PHP. [PHP Simple HTML DOM Parser](https://simplehtmldom.sourceforge.io/) is pretty nice – Cid Jan 08 '20 at 13:33

1 Answers1

0

Try with this pattern :

/<table ([^>]*?)width=["\']\s?\d+\s?(px|%)?\s*["\']/

The replacement $1 would preserve any other attributes (cf. (.*?) capture group) :

$text = preg_replace('/<table (.*?)width=["\']\s?\d+\s?(px|%)?\s*["\']/', '<table $1width="100%"', $text);
EricLavault
  • 12,130
  • 3
  • 23
  • 45
  • Won't work if input string is `