简体   繁体   中英

how to get all href links from web url

I want to get all href links from a web url(url: http://localhost/test.php ) including file also because i have included one php file (menulink.php) my script is working only for outside of href and if condition checked href only.

<a href="test.html" title="test"></a>
<a href="test1.html" title="test1"></a>
<a href="menulink1.html" title="menulink1"></a>
<a href="menulink2.html" title="menulink2"></a>

if condition true:

 <a href="test4.html" title="test4"></a>
 <a href="menulink3.html" title="menulink3"></a>

but i want to get else condition also both time(i need all href links do not consider if else condition or whatever condition)

Missing href links:

<a href="http://www.google.com" title="test6"></a>
<a href="menulink5.html" title="menulink5"></a>
<a href="menulink6.html" title="menulink6"></a>

how can i do it?can you resolve from my code. This is my code:

index.html

         $(document).ready(function(){

        var url = "http://localhost/links/test.php"; 
        var arr= null;
         $.post('load.php', { url: url,async: false},
            function(html) {
                $('#page').html(html);  
                var links = $('a');
                var title = $('a').attr('title');  

                var arr=[];
                for(var i=0; i < links.length; i++)
                {
                    arr.push(links[i]); 
                }
                console.log(arr); //all href links//  
        }  );

    });

test.php

  <html>
  <head>
  <title>Test</title>
  </head>
  <body>
  <div>
  <?php include("menulink.php"); ?>

   <a href="test.html" title="test"></a>
   <a href="test1.html" title="test1"></a>
   <?php
   $result_array="123";
   if ($result_array=="123")
   {
   ?>
   <a href="test4.html" title="test4"></a>
   <?php 
   }
   else{
   ?>
   <a href="http://www.google.com" title="test6"></a>
   <?php
   } 
   ?>
   </div>
   </body>
   </html>

load.php

<?php
$url = $_POST['url'];
$html = file_get_contents($url);
echo $html;
?>

menulink.php

  <html>
  <head>
  <title>Test</title>
  </head>
  <body>
  <div>
  <a href="menulink1.html" title="menulink1"></a>
  <a href="menulink2.html" title="menulink2"></a> 
  <?php
  $result_array="123";
  if ($result_array=="123")
  {
  ?>
  <a href="menulink3.html" title="menulink3"></a>

  <?php 
  }
  else{
  ?>
  <a href="menulink5.html" title="menulink5"></a>
  <a href="menulink6.html" title="menulink6"></a>
  <?php
  }
  ?>

 </div>
 </body>
 </html>

i want to get all the href links. Do not consider if else conditions or whatever conditions from the url document.please resolve from my code.

Try this :

     $dom = new DOMDocument;
     $dom->validateOnParse = true;
     $dom->loadHTML($html);
     $links = array(); 
     foreach($dom->getElementsByTagName('a'); as $link) { 
        $links[] = $link->getAttribute('href'); 
     } 
// Create DOM from URL or file //you have to include simple_html_dom.php form above link

$html = file_get_html('http://www.google.com/');



// Find all links 

You have to do this in load.php file as

         include("simple_html_dom.php");  // please make sure right location of file 
$url = $_POST['url'];
$html = file_get_html($url);
header('Content-Type: application/json');
$links = array(); 
foreach($html->find('a') as $element) {
    $links[] = $element->href;
    } echo json_encode($links);

and in jquery get those links

 $(document).ready(function(){

        var url = "http://localhost/links/test.php"; 
        //var arr= null;
         $.post('load.php', { url: url,async: false},
            function(html) {
                $('#page').html(html);  
                console.log(html);
        }  );

    });

HTML

        <html>
      <head>
      <title>Test</title>
      </head>
      <body>
      <div>


      <?php include("menulink.php"); ?>

       <a href="test.html" title="test"></a>
       <a href="test1.html" title="test1"></a>
       <?php
       get_links("123");
       get_links("321");
        function get_links($a){
            ?>
            <?php
       $result_array=$a;
       if ($result_array=="123")
       {
       ?>
       <a href="test4.html" title="test4"></a>
       <?php 
       }
       else{
       ?>
       <a href="http://www.google.com" title="test6"></a>
       <?php
       } 
       ?>
            <?php
        }
}
       ?>
       </div>
       </body>
       </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM