Add a None display to a div

Asked

Viewed 849 times

1

I’m trying to do a preg_replace to add a style in the id="content" div, but I didn’t have much success, someone would have a better idea?

Trying

$html = preg_replace('/<div id="conteudo">.*<\/div>/s','<div id="conteudo" style="display:none;">',$html);
echo $html

Page.php

<div id="conteudo">
    <div class="row" style="margin: 0; ">
        <div class="col-md-12" style="text-align:center; padding:8px;">
            <label style="color:white;"><b> Conteudo</b></label>
         </div>
    </div>
</div>

2 answers

2


Contrary to what was said in the other reply /a/396042/3635, it can rather parse strings in DOM and set values in attributes, such as style="", but regex is a tricky way to do it, there’s no reason to reinvent the wheel:

$html = '... Conteudo do HTML ...';

$doc = new DOMDocument();
$doc->loadHTML($html);

// "conteudo" é o ID, não adicione o #
$tagConteudo = $doc->getElementById('conteudo');

$tagConteudo->setAttribute('style', 'display: none;');

No Javascript and no "parses"

Before trying anything I must say, you do not need to set the style="", using jQuery, Javascript or parsing the DOM is unnecessary work (actually in this case using this is a huge exaggeration), just use the tag <style> and concatenate with the string in the variable $html you already have, simple example:

$sumir = '<style>
#conteudo {
    display: none !important;
}
</style>';

$html = $html . $sumir;
echo $html;

Note: put the !important to prevent other style sheets from having priority

0

The PHP is a server language, that is, it does not manipulate elements on the page but data that are processed in the server. She is not fit to do what you are trying, in which case I advise you to do via JS/Jquery.

$("#conteudo").attr("style","display:none");

That solves your problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.