简体   繁体   中英

How to get the href content of a link tag

I have a link tag. I want to get the href so that I can get the external CSS code.

This is what I tried:

<link rel="stylesheet" href="CSS/main.css" type="text/css">
<?php

    include('simple_html_dom.php');  
    $html = new simple_html_dom();  

    $html->load_file("test.txt");

    $file = fopen("link.txt","w");

    $link=$html->find("link");
    foreach($link AS $lk){
     $lk->href;

    $line_string=file_get_contents($lk);
    fwrite($file,($line_string. PHP_EOL));
    }
    fclose($file);
?>

Your line "$lk->href" isn't doing anything. Try assigning it to a variable and writing that variable. For example:

foreach($link AS $lk){
   $href = $lk->href;
   $line_string=file_get_contents($href);
   fwrite($file,($line_string. PHP_EOL));
}

you're not assigning the lk value to anything

$lk->href;

that returns the value of the href but doesn't assign it to anything. should be more like:

$link=$html->find("link");
foreach($link AS $lk){
 $hr=$lk->href;

$line_string=file_get_contents($hr);
fwrite($file,($line_string. PHP_EOL));
}

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