We help IT Professionals succeed at work.

Accessing specific element - DOM PHP HTML

weekapaug
weekapaug asked
on
131 Views
Last Modified: 2017-03-08
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

CERTIFIED EXPERT

Commented:
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?
CERTIFIED EXPERT

Commented:
<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?

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
Author of the Year 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions