php – preg_replace isn’t working while it does on other sections

I have the code below to filter out everything between <script> and <form> tags in a string.

// $Ntext = input from contenteditable; //
$Ntext = preg_replace('#(<script.*?>).*?(</script>)#', '$1$2', $Ntext);
$Ntext = preg_replace('#(<form.*?>).*?(</form>)#', '$1$2', $Ntext);

After this I retrieve the string to check and everything between the script tags is removed. But everything between the form tags isn’t deleted.

The weird thing is that I use this also on posting a new item and it works. But now I’m making the edit page, but with the new posting page as it’s base. They are the same, It can be that the content of the post is different but that’s also why I want to delete everything between the form tags. I noticed the error and that’s why I added the script removal part to test and that is working.

To be extra clear, I gonna give the input for new item and then the output.

<div id="CMformID_6bec7e3454-1" contenteditable="false"><form><br><br><div class="CMsett" contenteditable="false"><div class="snipDel" cm-form="CMformID_6bec7e3454-1" title="Delete this snippet?">X</div><select name="idLanguage1" id="idLanguage_6bec7e3454-1" class="CMlangSele" title="Select snippets language"><option value="text/html">HTML</option><option value="text/css">CSS</option><option value="text/javascript">Javascript</option><option value="application/x-httpd-php">PHP</option><option value="text/x-mysql">MySQL</option></select></div><textarea class="CodeDumpSnip" id="CodeItem_6bec7e3454-1" style="display: none;"></textarea><div class="CodeMirror cm-s-eclipse CodeMirror-wrap" translate="no"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 62.6px; left: 30.3438px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; min-height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" tabindex="-1" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" tabindex="-1" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: -17px; border-right-width: 33px; min-height: 94px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre class="CodeMirror-line-like"><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors" style="visibility: hidden;"><div class="CodeMirror-cursor" style="left: 29.3438px; top: 57.6px; height: 28.7969px;">&nbsp;</div></div><div class="CodeMirror-code" role="presentation"><pre class=" CodeMirror-line " role="presentation" style="text-indent: 0px; padding-left: 4px;"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">$</span>(<span class="cm-string">'#fff'</span>).<span class="cm-property">click</span>(<span class="cm-keyword">function</span>(<span class="cm-def">e</span>){</span></pre><pre class=" CodeMirror-line " role="presentation" style="text-indent: -33.7812px; padding-left: 37.7812px;"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text=" "> </span><span class="cm-variable">alert</span>(<span class="cm-string">'ok'</span>);</span></pre><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation" style="text-indent: 0px; padding-left: 4px;"><span role="presentation" style="padding-right: 0.1px;">});</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 33px; width: 1px; border-bottom: 0px solid transparent; top: 94px;"></div><div class="CodeMirror-gutters" style="display: none; height: 127px;"></div></div></div> <script> var editor1 = CodeMirror.fromTextArea(document.getElementById("CodeItem_6bec7e3454-1"), { mode: "text/html", theme: "eclipse", lineWrapping: true, styleActiveLine: true, matchBrackets: true, readOnly: false }); var charWidth = editor1.defaultCharWidth(), basePadding = 4; editor1.on("renderLine", function(cm, line, elt) { var off = CodeMirror.countColumn(line.text, null, cm.getOption("tabSize")) * charWidth; elt.style.textIndent = "-" + off + "px"; elt.style.paddingLeft = (basePadding + off) + "px"; }); editor1.on("change", function() { var update1 = editor1.getValue(); $("#CodeItem_6bec7e3454-1").val(update1); }); editor1.refresh(); $("#idLanguage_6bec7e3454-1").change(function(){ editor1.setOption("mode", $(this).val() ); }); editor1.on("paste", function(cm, event) { editor1.preventDefault(); } ); </script></form></div><br>

Then the output is:

<div id="CMformID_6bec7e3454-1" contenteditable="false"><form></form></div><br>

Now for the update page(same as new only different name) the input:

<div id="CMformID_3e3632d279-1" contenteditable="false"><br><form><div class="CMsett" contenteditable="false"><div class="snipDel" cm-form="CMformID_3e3632d279-1" title="Delete this snippet?">X</div><select id="idLanguage_3e3632d279-1" class="CMlangSele"><option value="text/html">HTML</option><option value="text/css" d="">CSS</option><option value="text/javascript" selected="">Javascript</option><option value="application/x-httpd-php">PHP</option><option value="text/x-mysql">MySQL</option></select></div> <textarea class="CodeDumpSnip" id="CodeItem_3e3632d279-1" style="display: none;">$('#fff').click(function(e){ alert('ok'); });</textarea><div class="CodeMirror cm-s-eclipse CodeMirror-wrap" translate="no"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5px; left: 5px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; min-height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" tabindex="-1" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" tabindex="-1" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: -17px; border-right-width: 33px; min-height: 94px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre class="CodeMirror-line-like"><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 4px; top: 0px; height: 28.7969px;">&nbsp;</div></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation" style="text-indent: 0px; padding-left: 4px;"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">$</span>(<span class="cm-string">'#fff'</span>).<span class="cm-property">click</span>(<span class="cm-keyword">function</span>(<span class="cm-def">e</span>){</span></pre></div><pre class=" CodeMirror-line " role="presentation" style="text-indent: -33.7812px; padding-left: 37.7812px;"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text=" "> </span><span class="cm-variable">alert</span>(<span class="cm-string">'ok'</span>);</span></pre><pre class=" CodeMirror-line " role="presentation" style="text-indent: 0px; padding-left: 4px;"><span role="presentation" style="padding-right: 0.1px;">});</span></pre></div></div></div></div></div><div style="position: absolute; height: 33px; width: 1px; border-bottom: 0px solid transparent; top: 94px;"></div><div class="CodeMirror-gutters" style="display: none; height: 127px;"></div></div></div> </form><script>var editor1 = CodeMirror.fromTextArea(document.getElementById('CodeItem_3e3632d279-1'), { mode: 'text/javascript', theme: 'eclipse', lineWrapping: true, styleActiveLine: true, matchBrackets: true, readOnly: false }); var charWidth = editor1.defaultCharWidth(), basePadding = 4; editor1.on('renderLine', function(cm, line, elt) { var off = CodeMirror.countColumn(line.text, null, cm.getOption('tabSize')) * charWidth; elt.style.textIndent="-" + off + 'px'; elt.style.paddingLeft = (basePadding + off) + 'px'; }); editor1.on('change', function() { var update1 = editor1.getValue(); $('#CodeItem_3e3632d279-1').val(update1); }); editor1.refresh(); $('#idLanguage_3e3632d279-1').change(function(){ editor1.setOption('mode', $(this).val() ); }); </script></div><br>

What I get as output after the replace on this page:

<div id="CMformID_3e3632d279-1" contenteditable="false"><br><form><div class="CMsett" contenteditable="false"><div class="snipDel" cm-form="CMformID_3e3632d279-1" title="Delete this snippet?">X</div><select id="idLanguage_3e3632d279-1" class="CMlangSele"><option value="text/html">HTML</option><option value="text/css" d="">CSS</option><option value="text/javascript" selected="">Javascript</option><option value="application/x-httpd-php">PHP</option><option value="text/x-mysql">MySQL</option></select></div> <textarea class="CodeDumpSnip" id="CodeItem_3e3632d279-1" style="display: none;">$('#fff').click(function(e){ alert('ok'); });</textarea><div class="CodeMirror cm-s-eclipse CodeMirror-wrap" translate="no"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5px; left: 5px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; min-height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" tabindex="-1" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" tabindex="-1" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: -17px; border-right-width: 33px; min-height: 94px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre class="CodeMirror-line-like"><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 4px; top: 0px; height: 28.7969px;">&nbsp;</div></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation" style="text-indent: 0px; padding-left: 4px;"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">$</span>(<span class="cm-string">'#fff'</span>).<span class="cm-property">click</span>(<span class="cm-keyword">function</span>(<span class="cm-def">e</span>){</span></pre></div><pre class=" CodeMirror-line " role="presentation" style="text-indent: -33.7812px; padding-left: 37.7812px;"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text=" "> </span><span class="cm-variable">alert</span>(<span class="cm-string">'ok'</span>);</span></pre><pre class=" CodeMirror-line " role="presentation" style="text-indent: 0px; padding-left: 4px;"><span role="presentation" style="padding-right: 0.1px;">});</span></pre></div></div></div></div></div><div style="position: absolute; height: 33px; width: 1px; border-bottom: 0px solid transparent; top: 94px;"></div><div class="CodeMirror-gutters" style="display: none; height: 127px;"></div></div></div> </form><script></script></div><br>

Can someone tell me what I’m doing wrong, didn’t see, or didn’t know?

Leave a Comment