1

I am working on one assignment in which I want to display particular <div> tag on basis of, if the user is on or off and up-user or down user. Here is my html code on name dom.html I also want to take care of nested and unnested content

<html>
<head>
    <title>Welcome to gaming website</title>
</head>
<body>
<div class="on">
    <p>You are in on class</p>
</div>
<div class="off">
    <div class="up-user">
        <p>you are in off up-user class</p>
    </div>
    <div class="down-user">
        <p>you are in off down-user class</p>
    </div>
</div>
</body>
</html>

I wrote the following php code in order to achieve this goal.

<?php
$myjson = '{"user": "down-user", "status": "on"}';
$result =json_decode($myjson, true);
$user = $result["user"];
$status=  $result["status"];
$html = file_get_contents('dom.html');
if ($user == 'down-user'  and $status == 'on'){
    $down-user-off= explode( '<div class="on">' , $html);
    $down-user-off-end = explode("</div>" , $down-user-off[1] );
    echo $down-user-off-end[0];
}elseif ($user == 'up-user'  and $status == 'off'') {
    $up-user-off = explode( '<div class="up-user">' , $html );
    $up-user-off-end = explode("</div>" , $up-user-off[1] );
    echo $up-user-off-end[0];
}

This gives me only the content inside div and do not display the html head and body tags. It is possible to display all the code i.e. html heat body and skip a particular div tag. For example I want to skip

<div class="on">
    <p>You are in on class</p>
</div>

from the code and display all the other html code.

Fun Maza
  • 13
  • 4

1 Answers1

0

You can use SimpleXMLElement and parse the html as XML with xpath selectors.

$xmlWrapper = new SimpleXMLElement($html);
if ($user == 'down-user'  and $status == 'on'){
    $onElements = $xmlWrapper->xpath('//div[@class="on"]');
    if (is_array($onElements) && count($onElements)) {
        $onElement = $onElements[0];
        echo 'on : '.$onElement->asXML();
    }
}
//and so on for the other cases...

You can experiment with this code in this php fiddle

Edit:

In your feedback, you mentioned you wanted to maintain the other parts of the html (e.g. header with style tags), etc...So in that mindset, you have a couple options (but these are not the only methods) - see the list below. Other options for templating include template libraries - e.g. Twig, plates, Smarty - see this article.

  1. Add conditional styles (to the header tag) based on the values of user and status:

    a. add a <style> tag to the header:

    <style> #{styles} </style>

    b. run a str_replace

    if ($user == 'down-user' and $status == 'on'){ $styles = '.on { display: block;} .off { display: none; }'; } else if ($user == 'up-user' and $status == 'off') { $styles = '.on, .down-user { display: none;} .off { display: block; }'; } str_replace('#{styles}',$styles,$page);

    (see phpfiddle for this)

  2. add the html main content conditionally:

    a. add a body template:

    <body>#{body}</body>

    b. replace the body template text based on the values of user and status:

    if ($user == 'down-user' && $status == 'on'){ //this could be saved in another file, //and pulled in using require, file_get_contents(), etc $bodyHTML = '<div class="on"> <p>You are in on class</p> </div>'; } else if ($status == 'off') { $bodyHTML = '<div class="off">'; if ($user == 'up-user') { $bodyHTML .= ' <div class="down-user"> <p>you are in off down-user class</p> </div>'; } else { $bodyHTML .= '<div class="up-user"> <p>you are in off up-user class</p> </div>'; } $bodyHTML .= '</div>'; } echo str_replace('#{body}',$bodyHTML,$page);

    (see phpfiddle example)

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • Hello, I appreciate your response. With this I can only get the p tag value. If you click on the source code it did not show the full html content, specially head tag in with I can add css. – Fun Maza Nov 17 '16 at 00:13
  • Yes exactly. I want to load all template. skill some tags on basis of classes assigned and display the required templates. Your above answer helped me. But still need to load the template. – Fun Maza Nov 17 '16 at 00:27
  • This solution is not good for security, a user and easily display content via inspect element. – Fun Maza Nov 18 '16 at 22:35
  • I was considering mentioning that regular expressions might provide a solution but that could lead to issues with greedy matching algorithms when the HTML structure gets complex. Then I realized I should suggest you use the SimpleXMLElement approach, but instead of finding the elements to display, find the elements that should not be displayed, remove them (using the technique offered in the answer to [this question](http://stackoverflow.com/questions/262351/remove-a-child-with-a-specific-attribute-in-simplexml-for-php)) and then show the resulting HTML at the end. – Sᴀᴍ Onᴇᴌᴀ Nov 20 '16 at 15:48
  • I used DOMDocument() and xpath. – Fun Maza Nov 20 '16 at 18:23