PHP cURL返回远程页面样式表 [英] PHP cURL to return remote page stylesheets

查看:69
本文介绍了PHP cURL返回远程页面样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码通过PHP cURL获取远程内容

I'm using following code to get remote content using PHP cURL

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

此代码返回整个内容,但我只想以以下格式打印所有样式表。

This code returns whole content But I just want to print all stylsheets in following format.

<link rel="stylesheet" href="http://www.example.com/css/style1.css">
<link rel="stylesheet" href="http://www.example.com/css/style2.css">

如何使用str.replace()过滤内容以仅获取带有cURL的样式表?

How do I Filter content using str.replace() to only get stylsheets with cURL?

推荐答案

使用简单的html dom库

include('simple_html_dom.php');

// get DOM from URL or file
$html = file_get_html('http://www.example.com/');
// or your can get $html string through your curl request and say
// $html = str_get_html($html);

// find all "link"
foreach($html->find('link') as $e) {
    if($e->type="text/css" && strpos($e->href, ":/") !=== false) // you don't want relative css hrefs. right?
    echo $e->href."<br>";
}

这篇关于PHP cURL返回远程页面样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆