Note that there are some explanatory texts on larger screens.

plurals
  1. POcleaning contents inside of html tags
    text
    copied!<p>I am trying to write a preg_replace that will clean all tag properties of the allowed tags, and all tags that do not exist in the allowed list.</p> <p>Basic example- this: </p> <pre><code>&lt;p style="some styling here"&gt;Test&lt;div class="button"&gt;Button Text&lt;/div&gt;&lt;/p&gt; </code></pre> <p>would turn out to be:</p> <pre><code>&lt;p&gt;test&lt;/p&gt; </code></pre> <p>I have this working well.. Except for img tags and a href tags. I need to not clean the properties of the img and a tags. Possibly others. I was not sure if there was a way to set two allow lists? </p> <p>1) One list for what tags are allowed to stay after being cleaned<br> 2) One list for the tags that are allowed but left alone?<br> 3) The rest are deleted.</p> <p>Here is the script I am working on:</p> <pre><code>$string = '&lt;p style="width: 250px;"&gt;This is some text&lt;div class="button"&gt;This is the button&lt;/div&gt;&lt;br&gt;&lt;img src="waves.jpg" width="150" height="200" /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Title&lt;/b&gt;&lt;br&gt;Here is some more text and &lt;a href="#" target="_blank"&gt;this is a link&lt;/a&gt;&lt;/p&gt;'; $output = strip_tags($string, '&lt;p&gt;&lt;b&gt;&lt;br&gt;&lt;img&gt;&lt;a&gt;'); $output = preg_replace("/&lt;([a-z][a-z0-9]*)[^&gt;]*?(\/?)&gt;/i", '&lt;$1$2$3$4$5&gt;', $output); echo $output; </code></pre> <p>This script should clean the $string to be:</p> <pre><code>&lt;p&gt;This is some text&lt;br&gt;&lt;img src="waves.jpg" width="150" height="200" /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Title&lt;/b&gt;&lt;br&gt;Here is some more text and &lt;a href="#" target="_blank"&gt;this is a link&lt;/a&gt;&lt;/p&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload