Accessing specific element - DOM PHP HTML

weekapaug
weekapaug used Ask the Experts™
on
I am using the simple html DOM with PHP 7.0.

If i need to get at the pClassLarge value but set variable for each one, how would I do this??

<div class="outerClass"><p class="pClassTop">Subject One</p><p class="pClassLarge">I need this!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Two</p><p class="pClassLarge">I also need this value!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Three</p><p class="pClassLarge">And lastly this one too!!</p></div>

This code below is totally wrong, but I'm trying to express my intentions... Each pClassTop is  a different header and each pClassLarge within is a different value but I need to be sure I am isolating them.

$var1=(the value for pClassLarge within the pClass top IF <p>Subject One</p>);
$var2=(the value for pClassLarge within the pClass top IF <p>Subject Two</p>);
$var3=(the value for pClassLarge within the pClass top IF <p>Subject Three</p>);

Please excuse the rough code for the variables, as I have no idea where to begin, so I'm just trying to show the concept for now.  I am able to isolate by class, but then I'm just left with a bunch of digits that I have no idea what they belong to, so I need to set the variables for pClassLarge based on the subject contained within the pClassTop.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Please check the below code.



</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<body>
<div class="outerClass"><p class="pClassTop">Subject One</p><p class="pClassLarge">I need this!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Two</p><p class="pClassLarge">I also need this value!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Three</p><p class="pClassLarge">And lastly this one too!!</p></div>
</body>
</html>


<script>
      
      $(document).ready(function(){
    $('.pClassLarge').each(function(i, obj) {
       x = $(obj).html();
       alert(x);//this is the value of div
            alert(i);//this the index
    });
});
</script>

Author

Commented:
This does work, but its over my head.  I'm really new at javascript and was hoping to figure out how to do this in php.

Basically I understand its setting the value but its overwriting the old one.  I would need to store it as 3 different variables as in the question and I dont know what to do in javascript to go any further with this.  Any other ideas?
<div class="outerClass"><p class="pClassTop">Subject One</p><p class="pClassLarge">I need this!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Two</p><p class="pClassLarge">I also need this value!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Three</p><p class="pClassLarge">And lastly this one too!!</p></div>

From where do you get these tags generated from?

or "I need this!!" :  Do you get this value from a page generated using php code or is it a static html code?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Its an html page and this example shows that basically each <div> contains an inner <p>.  So in every case I need to assign by figuring out what the content of the div actually is then set the variable to the value of the inner <p> within the div.  For instance...if the <div> has content in it that says, "temperature" the <p>says "96 degrees".  Or the <div> says "favorite food" and the <p> says "pizza".

There are only a few fields I need so I can do something like...

if div contents="temperature" set $temperature=<the value of p>
if div contents="favorite food" set $favFood=<the value of p>

Each div class is identical and each p class is identical but by scanning for the value inside the div, I can assign the appropriate variable depending on which div its looking in.
Imran AliWeb Developer

Commented:
Following is solution to get all values from pClassLarge class:

This will give you array of values.

$html = '<div class="outerClass"><p class="pClassTop">Subject One</p><p class="pClassLarge">I need this!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Two</p><p class="pClassLarge">I also need this value!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Three</p><p class="pClassLarge">And lastly this one too!!</p></div>';

preg_match_all('/class="pClassLarge">(.*?)<\/p>/s',$html,$arrayMatches);

$classLargeValues = $arrayMatches[1];

print_r($classLargeValues);

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
You cannot parse HTML with regular expressions.  HTML is not a regular language, and while one or two isolated examples can be crafted, the regular expressions will fail in the real-world implementations.  This has been a well-known issue for a long time.

Beware that when you're working with an incomplete document or invalid markup, the simple_html_dom may experience recursion in the parent nodes, and this can lead to huge memory consumption and long processing times.  If you try this script and run out of memory, consider using a well-formed HTML document instead of just three div tags.  You can find out if you have a well-formed HTML document by using the HTML validator.
https://validator.w3.org/

Please see: https://iconoun.com/demo/temp_weekapaug.php
<?php // demo/temp_weekapaug.php
/**
 * https://www.experts-exchange.com/questions/29007422/Accessing-specific-element-DOM-PHP-HTML.html
 *
 * http://simplehtmldom.sourceforge.net/
 */
error_reporting(E_ALL);
echo '<pre>';

// LOAD THE DOM PARSER
require_once('simplehtmldom_1_5/simple_html_dom.php');

// THE TEST DATA FROM THE POST AT E-E
$htm = <<<EOD
<div class="outerClass"><p class="pClassTop">Subject One</p><p class="pClassLarge">I need this!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Two</p><p class="pClassLarge">I also need this value!!</p></div>
<div class="outerClass"><p class="pClassTop">Subject Three</p><p class="pClassLarge">And lastly this one too!!</p></div>
EOD;

/// GET THE HTML INTO A DOM OBJECT
$dom = str_get_html($htm);

// COLLECT THE OUTPUTS HERE
$pcts = [];
$pcls = [];

// USE ITERATORS TO FIND THE DATA
foreach ($dom->find('div[class=outerClass]') as $div_element)
{
    foreach ($div_element->find('p[class=pClassTop]') as $pct)
    {
        $pcts[] = $pct->plaintext;
    }

    foreach ($div_element->find('p[class=pClassLarge]') as $pcl)
    {
        $pcls[] = $pcl->plaintext;
    }
}

// SHOW THE WORK PRODUCT
var_dump($pcts, $pcls);

Open in new window

Outputs:
array(3) {
  [0]=>
  string(11) "Subject One"
  [1]=>
  string(11) "Subject Two"
  [2]=>
  string(13) "Subject Three"
}
array(3) {
  [0]=>
  string(13) "I need this!!"
  [1]=>
  string(24) "I also need this value!!"
  [2]=>
  string(25) "And lastly this one too!!"
}

Open in new window

Author

Commented:
Ray,

This looks  like what I need to do, I will play around with it.  

To finish I need to loop through and ask what is the value.  So for the example above its basically

Field Name = Field Value

$pct[0]=$pcl[0];
$pct[1]=$pcl[1];
$pct[2]=$pcl[2];

Below is conceptually what I'm trying to do with the completed arrays.

if (***$pct array value***==="Subject One"){
     $subjectOne="*** the matching value $pcl array***"
}elseif(***$pct array value***==="Subject Two"){
      $subjectTwo="*** the matching value $pcl array***"
}elseif(***$pct array value***==="Subject Three"){
      $subjectThree="*** the matching value $pcl array***"
}

I'm just not sure on the syntax to do this?  Also I loaded an entire html page, not just 3 divs and I did so like this

$html = file_get_html('myHTMLsaveInFile.txt');

It works fine to create the arrays, but is there a reason I should use the <<<EOD thing and if so how do incorporate a file into that such as

$htm = <<<EOD
file_get_html('myHTMLsaveInFile.txt'); 
EOD;

Open in new window


The above doesnt work but is it necessary if it works without the EOD thing?

Author

Commented:
Ray, this does exactly what I needed to know.   I went on to ask one more question which I'll work on and submit a new one if I can't figure it out.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial