0

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

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

MANnDAaR
  • 2,546
  • 7
  • 45
  • 64

4 Answers4

2

If you only want to leave the <link> elements intact then you can use PHP's strip_tags() function.

strip_tags — Strip HTML and PHP tags from a string

It accepts an additional parameter that defines allowed tags, so all you have to do is set the only allowed tag to be the <link> tag.

$output = curl_exec($ch);
$linksOnly = strip_tags($ouput,'link');

The main problem here is that you don't really know what content you are going to get and trying to parse HTML content with anything other than a tool designed for that task may leave you with grey hair and a nervious twitch ;)

References -

Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131
1

A better approach would be to use PHP DOM to parse the HTML tree and retrieve the required nodes - <link> in your case - and filter them appropriately.

WeaklyTyped
  • 1,331
  • 4
  • 16
  • 31
1

Using a regex:

preg_match_all('/rel="stylesheet" href="(.*)">/', $output, $matches);

if (isset($matches[1]) && count($matches[1]))
{
  foreach ($matches as $value)
  {
    echo '<link rel="stylesheet" href="'.$value.'">';
  }
}
j0k
  • 22,600
  • 28
  • 79
  • 90
1

Using simple html dom library,

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>";
}
Prasanth
  • 5,230
  • 2
  • 29
  • 61