Link to home
Start Free TrialLog in
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.
Avatar of Peos John
Peos John
Flag of Malaysia image

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>
Avatar of weekapaug
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?
<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?
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.
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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.