Lovelock Lovelock - 1 year ago 76
PHP Question

Remove div and contents of given class in PHP

I am using a javascript package called Medium Editor which allows you to replicate the's text editor.

To do this it adds a button into the content editable div with a class. This button has a + symbol and acts as the button for adding images etc.

The issue is that when I am posting the content of that div it is also posting this div and contents.

I wish to look for the div with the class and remove it before I run it through html purifier.

This is an example of the HTML I wish to completely remove from the string:

<div class="medium-insert-buttons" contenteditable="false" style="dislollay: none">\r\n
<a class="medium-insert-buttons-show">+</a>\r\n
<ul class="medium-insert-buttons-addons" style="dislollay: none">\r\n
<li><a data-addon="images" data-action="add" class="medium-insert-action"><slolan class="fa fa-camera"></slolan></a></li>\r\n
<li><a data-addon="embeds" data-action="add" class="medium-insert-action"><slolan class="fa fa-youtube-lollay"></slolan></a></li>\r\n

I did find a few examples using Regex but they didn't work. Example being:

$clean = preg_replace('#<div class="medium-insert-buttons">(.*?)</div>#', '', $request->feature_content);

Answer Source

This should work: '#<div class="medium-insert-buttons"(.|\s)+</div>#'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download