javascript - 使用PHP获取HTML标签内容

标签 javascript php jquery html

下面是我的示例 html 代码,

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

在此内容页面中,我通过以下 PHP 代码检索标签内容,

 <?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++;
?>

这样它将检索如下屏幕所示的内容

enter image description here

但是我想要按 h3 及其段落的顺序排列结果,然后是 h3 及其段落,我该怎么办。我不知道,我尝试了很多方法,有人请帮我解决这个问题。提前致谢。

最佳答案

尝试:

<?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
}

关于javascript - 使用PHP获取HTML标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40560443/

相关文章:

javascript - 将流程图另存为 PDF

PHP 代码 : Trying to link to a MYSQL table

PHP支付解决方案?

javascript - HTML jQuery .load() 不工作

javascript - 使用 Webpack + Jquery + bootstrap 进行 react

javascript - TinyMCE 正在添加新行 ("\n")而不管 apply_source_formatting

javascript - 如何使用 jsreport 和 jsreport Chrome Pdf 渲染 1k pdf 文件?

javascript - 如何更改包含连字符的 HTML 属性

php - 身份验证页面没有在 Laravel 中获取 css

javascript - 为什么我的开关 block (打开 $(e.target) .attr ("class"))不工作?