Kavya Shree
Kavya Shree

Reputation: 1032

Get HTML tag contents using PHP

Below is my sample html code,

 <section class="well well-2" style="background-color:#eee">
            <div class="container">
               <div class="row">
                  <div class="col-md-4 text-center">
                     <h3 style="color:#CC0000">Car Bumper</h3>
                     <p>Entrusting upon credible vendors for high-grade raw materials, we manufacture and supply premium quality Car Bumper.</p>
                     <a href="car_bumper.php" class="btn btn-lg btn-yellow-green">...More</a>        
                  </div>
                  <div class="col-md-4 text-center">
                     <h3 style="color:#CC0000">Car Body Kits</h3>
                     <p>Enriched with a vast industry experience, we are capable of manufacturing and supplying best quality Car Body Kits.</p>
                     <a href="car_bodykitss.php" class="btn btn-lg btn-yellow-green">...More</a>        
                  </div>
                  <div class="col-md-4 text-center">
                     <h3 style="color:#CC0000">Car Alteration</h3>
                     <p>We are a credible name that is occupied in the business of manufacturing and supplying of best quality Car Modification Kit.</p>
                     <a href="car_alteration.php" class="btn btn-lg btn-yellow-green">...More</a>        
                  </div>
               </div>
               <div class="row">
                  <div class="col-md-4 text-center">
                     <h3 style="color:#CC0000">Car Styling Kits</h3>
                     <p>We are one among the prominent manufacturers and suppliers of a wide array of Car Styling kits with perfect finishing.</p>
                     <a href="car_stylingkit.php" class="btn btn-lg btn-yellow-green">...More</a>        
                  </div>
                  <div class="col-md-4 text-center">
                     <h3 style="color:#CC0000">Car Painting Services</h3>
                     <p>Painting requires special expertises, we paint cars , which offers ultraviolet blockers in the clear coat.</p>
                     <a href="car_painting.php" class="btn btn-lg btn-yellow-green">...More</a>        
                  </div>
                  <div class="col-md-4 text-center">
                     <h3 style="color:#CC0000">Car Spoiler</h3>
                     <p>We are listed at the acme for manufacturing and supplying an ample range of best quality Car Spoilers.</p>
                     <a href="car_spoiler.php" class="btn btn-lg btn-yellow-green">...More</a>        
                  </div>
               </div>
            </div>
         </section>

From this Page of content I retrieve tag content via below PHP code,

 <?php
  include('simple_html_dom.php');
  $filename='index.php';
  $dom = file_get_contents($filename);
  $html = file_get_html($filename);
$h3=array();
$p=array();
$tags = get_meta_tags($filename);
foreach($html->find('h3') as $element) { ?>
    <div class="form-group">
      <label class="control-label col-sm-2" for="pwd">H3</label>
      <div class="col-sm-10">
        <input type="text" class="form-control" name="h3[]" id="h3[]" value="<?php echo $element->innertext; ?>">
      </div>
    </div>
    <?php  $k++; foreach($html->find('p') as $element) {?>
    <div class="form-group">
      <label class="control-label col-sm-2" for="pwd">Paragraph</label>
      <div class="col-sm-10">
        <input type="text" class="form-control" id="p1[]" name="p1[]" value="<?php echo $element->innertext; ?>">
      </div>
    </div>
    <?php } $o++;
?>

So that It will retrieve content like below screen

enter image description here

But I Want the result in order of h3 and its paragraph and next its h3 and its paragraph how can I do with this.I dont know And I tried in many way anyone please help me solve this prblm.Thanks in advance.

Upvotes: 0

Views: 76

Answers (2)

TDWebDev
TDWebDev

Reputation: 84

Have you tried using separate variables for the 2 loops? I see you're using $element for both loops that just seems inappropriate.

Upvotes: 0

MurthyAdapa
MurthyAdapa

Reputation: 121

Try:

<?php

include('simple_html_dom.php');
$filename='index.php';
$html = file_get_html($filename);

$h3 = array();
foreach ($html->find('h3') as $element) {
    $h3[] = $element->innertext;
}

$p = array();
foreach ($html->find('p') as $element) {
    $p[] = $element->innertext;
}

for ($i = 0; $i <= count($h3); $i++) {
    ?>
    <div class = "form-group">
        <label class = "control-label col-sm-2" for = "pwd">H3</label>
        <div class = "col-sm-10">
            <input type = "text" class = "form-control" name = "h3[$i]" id = "h3[$i]" value = "<?php echo $h3[$i] ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-sm-2" for="pwd">Paragraph</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="p[$i]" name="p[$i]" value="<?php echo $p[$i] ?>">
        </div>
    </div>
    <?php
}

Upvotes: 2

Related Questions