Accessing specific element - DOM PHP HTML

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.
weekapaugAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peos JohnPHPCommented:
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>
0
weekapaugAuthor 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?
0
Peos JohnPHPCommented:
<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?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

weekapaugAuthor 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.
0
Imran AliWeb DeveloperCommented:
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

0
Ray PaseurCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
weekapaugAuthor 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?
0
weekapaugAuthor 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.