K
K
karetsky2015-09-24 09:49:54
PHP
karetsky, 2015-09-24 09:49:54

What is the correct way to build a regular expression preg_replace?

Not strong in regulars. Help me compose a regular expression to remove all attributes/styles from tags, except for tags: img,table,td,tr,a
I only got as far as removing all attributes myself:

$html_no_attr = preg_replace('#(</?\w+)(?:\s(?:[^<>/]|/[^<>])*)?(/?>)#ui', '$1$2', $html);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Optimus, 2015-09-24
Pyan @marrk2

If you need to carefully (for example, you want to keep align= in the code), then so
If it's hardcore, it's like this:

$text = preg_replace("/<div(.*?)>/ism", "<div>", $text);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question