1

Is there a function in PHP that does the same job as jQuery's remove()? I have this simple javascript function....

    function remove_trs (){
        $( "tr" ).remove( ".problem_display_work" );
    }

which removes all table ,<tr>'s that are class="problem_display_work" but I want to see if this can be done in PHP.

EDIT FOR CLARIFICATION

Ultimately, I'm having some HTML loaded in to a TinyMCE editor. The function above is called via onclick of a button. This all works well when the HTML is outside the TinyMCE editor, but it doesn't work when I place the HTML content inside the editor. Thoughts?

Thanks.

  • What you want to achieve by doing this via `PHP` – Apoorv Jun 16 '16 at 13:26
  • you cannot manipulate existing (runned on browser) html code via php, only javascript (which is a language that runs after the page is loaded) can do that. What you can do, if you crate the row within php foreach for instance, is make an if to test a condition and if that if == false then not write the TR at all – Ares Draguna Jun 16 '16 at 13:26
  • This can be done with something like DOMDocument (native php). But it seems like a bad design, except for external HTML that you can't modify. – FLX Jun 16 '16 at 13:27
  • Do you want to remove tags from a HTML string in PHP? If so, you'll need to look into regular expressions. You'll probably need to work with look ahead/look behind, so building the regex might take some fidling, but it can be done. – Mike Scotty Jun 16 '16 at 13:29
  • @FLX exactly :) there is basically no point in doing it via PHP, in my opinion... it would take allot more time to execute then that javascript function... and the DOMDocument of PHP does not do anything for HTML that's already displayed by the browser – Ares Draguna Jun 16 '16 at 13:29
  • @AresDraguna +1. HTML should be generated without the problematic from the beginning – FLX Jun 16 '16 at 13:32
  • @mpf82 - no, [regex would not be the correct answer to that question](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). – Simba Jun 16 '16 at 13:39

2 Answers2

1

You can set your class as a PHP variable in your HTML code and change the class name.

This is an example in the <a> tag but you can use it in your <tr> tag

adding and removing css classes on href

Community
  • 1
  • 1
Andres
  • 389
  • 1
  • 6
  • 19
1

The code in your TinyMCE is not part of the DOM, it's a string.

But Thanks to JQuery, you can manipulate HTML strings as if they were part of the DOM.

Click the Do Stuff Str Button, to see how the table's content as string gets manipulated.

You need to adapt this example to work with the string from your TinyMCE and write the string back to the editor when you're done.

$(document).ready(function() {

  function remove_trs() {
    $("tr").remove(".problem_display_work");
  }

  function remove_trs_str(str) {
    // pass your HTML string to this function
    $str = $(str);
    $(".problem_display_work", $str).remove();
    return $str;
  }

  $("#do_stuff_dom").click(function() {
    remove_trs();
  });

  $("#do_stuff_str").click(function() {
    var html_str = $("#my_tbl").html(); // string from your TinyMCE 
    var $ret = remove_trs_str(html_str);
    console.log($ret.html());
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div>
  <table id="my_tbl">
    <tr class="problem_display_work">
      <td>this will be removed</td>
    </tr>
    <tr class="something_else">
      <td>this will be kept</td>
    </tr>
  </table>
</div>

<button id="do_stuff_dom">Do Stuff DOM</button>
<button id="do_stuff_str">Do Stuff Str</button>

There's really no need to pass the HTML to PHP to do what you want.

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50