title: Remove attributes from html tags using PHP while keeping specific attributes
body: <p>I <a href="https://stackoverflow.com/a/3026235/937471">found a way</a> to remove all tag attributes from a html string using php:</p>
  
  <pre><code>$html_string = "&lt;div class='myClass'&gt;&lt;b&gt;This&lt;/b&gt; is an &lt;span style='margin:20px'&gt;example&lt;/span&gt;&lt;img src='ima.jpg' /&gt;&lt;/div&gt;";
  $output = preg_replace("/&lt;([a-z][a-z0-9]*)[^&gt;]*?(\/?)&gt;/i",'&lt;$1$2&gt;', $html_string);
  echo $output;
  //&lt;div&gt;&lt;b&gt;This&lt;/b&gt; is an &lt;span&gt;example&lt;/span&gt;&lt;img/&gt;&lt;/div&gt;
  </code></pre>
  
  <p>But I would like to keep certain tags such as src and href. I have almost no experience with regular expresions, so any help would be really appreciated.</p>
  
  <p>[maybe] Relevant update: This is parto of a process of 'cleaning' posts on a database. I am iterating through all the posts, getting the html, cleaning it, and updating it on the corresponding table.</p>