• UPDATE mytabl...">
    Clever Geek Handbook

    PHP: removing multiple child tags

    <div class="article_content">
     <p>&nbsp;</p>
     <p>&nbsp;</p>
     <p>&nbsp;</p>
     <p>&nbsp;</p>
    
    <ul>
     <li>UPDATE mytable SET tax = amount</li>
    </ul>
    <p>after you can remove it</p>
    <p>&nbsp;</p> <!-- dot want to delete this line -->
    <ul><li>ALTER TABLE mytable DROP COLUMN amount;</li>
    </ul>
    </div>
    
          

    I want to remove everything <p>&nbsp;</p>

    until the first ul

    or any tag appears for example <p>.... content .... </p>

    .

    If I'm going to use this str_replace("<p>&nbsp</p>","",$string);

    one it will remove all blank lines from the string. But I want to delete lines before the first occurrence.

    +3
    php regex


    user7891160 Apr 27. 17 at 4:21
    source to share


    6 answers


    Hopefully this helps you by deleting all the child nodes it starts at &nbsp;

    until a new tag appears with some content.

    Try this piece of code here



    <?php
    ini_set('display_errors', 1);
    header("Content-Type:text/html; charset=UTF-8");
    $string= <<<HTML
    <html><body><div class="article_content">
     <p>&nbsp;</p>
     <p>&nbsp;</p>
     <p>&nbsp;</p>
     <p>&nbsp;</p>
    
    <ul>
     <li>UPDATE mytable SET tax = amount</li>
    </ul>
    <p>after you can remove it</p>
    <p>&nbsp;</p> <!-- dot want to delete this line -->
    <ul><li>ALTER TABLE mytable DROP COLUMN amount;</li>
    </ul>
    </div></body></html>
    HTML;
    $object = new DOMDocument();
    $object->loadHTML($string);
    $remove=array();
    $nodelist=$object->getElementsByTagName("div")->item(0)->childNodes;
    foreach($nodelist as $node)
    {
        if($node instanceof DOMElement)
        {
            if($node->tagName=='p' && str_replace("&nbsp;","",htmlentities($node->textContent))=="")
            {
                $remove[]=$node;
            }
            else
            {
                break;
            }
        }
    }
    foreach($remove as $node)
    {
        $node->parentNode->removeChild($node);
    }
    echo $object->saveHTML();
    
          

    0


    Sahil gulati Apr 27. 17 at 5:21
    source to share


    I think you should try jQuery instead of PHP because jQuery is the best library for moving and manipulating the DOM.

    Try the snippet below, hope it is better for you.



    $(function() {
      $(".article_content p").each(function() {
        var self = $(this),
          arr = []; // get all blank elements in array
        if (self.text().trim().length == 0 // check its length
          &&
          (self.prev().text().trim().length === 0 || // checking previous element is blank or not
            self.next().text().trim().length === 0)) { // checking next element is blank or not
          arr.push(self); // if all are blank then add in array
        }
        // now remove all elements
        $(arr).each(function() {
          $(this).remove()
        });
      });
    });
          

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="article_content">
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <ul>
        <li>UPDATE mytable SET tax = amount</li>
      </ul>
      <p>after you can remove it</p>
      <p>&nbsp;</p>
      <!-- dot want to delete this line -->
      <ul>
        <li>ALTER TABLE mytable DROP COLUMN amount;</li>
      </ul>
    </div>
          

    Run codeHide result


    0


    Rohan kumar Apr 27. 17 at 4:36 am
    source to share


    It might be possible client side like jQuery

    • ... prevAll () : - Get the immediately preceding sibling of each element in the set of matched elements. If a selector is provided, it retrieves the previous sibling only if it matches that selector.
    • ... first () : - Reduce the set of matched elements to the first in the set.

    Example shown below: -

    $(function() {
    var pTag = $( "ul" ).first().prevAll('p');
    pTag.remove();
    });
          

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <div class="article_content">
     <p>&nbsp;</p>
     <p>&nbsp;</p>
     <p>&nbsp;</p>
     <p>&nbsp;</p>
    
    <ul>
     <li>UPDATE mytable SET tax = amount</li>
    </ul>
    <p>after you can remove it</p>
    <p>&nbsp;</p> <!-- dot want to delete this line -->
    <ul><li>ALTER TABLE mytable DROP COLUMN amount;</li>
    </ul>
    </div>
          

    Run codeHide result


    0


    Aman kumar Apr 27. At 4:39 am
    source to share


    You can go the same way when inserting data into the database. The default creates a paragraph element each time you press Enter: Change the default CKEDITOR setting.

    config.autoParagraph = false;
    
          

    and also you can set the input mode as BR and other appropriate

    config.enterMode = CKEDITOR.ENTER_BR;
    
          

    Read this same question

    0


    Ahmed Ginani Apr 27. 17 at 4:44
    source to share


    // Press F11 to toggle full screen editting (Ctrl+โŒ˜+F on Mac OS).
    // Press Ctrl+Shift+F to format code.
    $string = '
    <div class="article_content">
     <p>&nbsp;</p>
     <p>&nbsp;</p>
     <p>&nbsp;</p>
     <p>&nbsp;</p>
    
    <ul>
     <li>UPDATE mytable SET tax = amount</li>
    </ul>
    <p>after you can remove it</p>
    <p>&nbsp;</p> <!-- dot want to delete this line -->
    <ul><li>ALTER TABLE mytable DROP COLUMN amount;</li>
    </ul>
    </div>
    ';
    $remove=array();
    $dom = new DOMDocument;
    $dom->loadHTML($string);
    //$newstring = $dom->getElementsByTagName('<p>&nbsp;</p>')->item(4);
    $newstring = $dom->getElementsByTagName('p');
    foreach($newstring as $stringnode)
    {
            if($stringnode->tagName=='p' && str_replace("&nbsp;","",htmlentities($stringnode->textContent))=="")
            {
                $remove[]=$stringnode;
            }
            else
            {
                break;
            }
    }
    
    foreach($remove as $node)
    {
    
            $node->parentNode->removeChild($node);
    
    }
    echo $dom->saveHTML();
    
          

    0


    Maulik patel Apr 27. 17 at 6:44 am
    source to share


    It is good practice to use the DOM when working with HTML structure. But in this particular case, unlike other answers, I prefer regexes more:

    (?s)<(?:(?:ul|p>(?:(?!&nbsp;)|[^>]*</p>\s*(*ACCEPT)))).*\K
    
          

    Live demo

    Regex explanation:

    (?s)    # Enable DOTALL modifier
    <       # Match a `<`
        (?:     # Start of non-capturing group (a)
            (?: # Start of NCG (b)
                ul  # Match `ul`
                |   # OR
                p>  # Match `p>`
                (?: # Start of NGC (c)
                    (?!&nbsp;)   # Shouldn't be followed by `&nbsp;`
                    |            # OR
                    [^>]*</p>\s* # Otherwise match whole `p` tag
                    (*ACCEPT)    # Force engine to end current matching attempt
                )   # End of NGC (c)
            )   # End of NGC (b)
        ).*\K   # End of NGC (a), match up to the end of input string and throw it away
    
          

    PHP code:

    echo preg_replace('~(?s)<(?:(?:ul|p>(?:(?!&nbsp;)|[^>]*</p>\s*(*ACCEPT)))).*\K~', '', $html);
    
          

    PHP Live Demo

    0


    revo Apr 27. 17 at 10:54
    source to share






    More articles:

      Removed postRemoved postRemoved postRemoved postRemoved postRemoved postRemoved postRemoved postRemoved postRemoved post

    All Articles

    Daily Blog | 2020

    Green Geek Media (GGM)
    1298 Despard Street
    GA 30344 East Point, USA
    404-763-3837