Avatar of weekapaug
weekapaug
 asked on

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.
HTMLPHP* DOM

Avatar of undefined
Last Comment
weekapaug

8/22/2022 - Mon
Peos John

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>
weekapaug

ASKER
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?
Peos John

<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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
weekapaug

ASKER
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 Ali

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

ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
weekapaug

ASKER
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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
weekapaug

ASKER
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.