简体   繁体   English

如何获取链接标签的href内容

[英]How to get the href content of a link tag

I have a link tag. 我有一个link标签。 I want to get the href so that I can get the external CSS code. 我想获取href以便获取外部CSS代码。

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. 您的行“ $ lk-> href”没有做任何事情。 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值分配给任何东西

$lk->href;

that returns the value of the href but doesn't assign it to anything. 会返回href的值,但不会将其分配给任何内容。 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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