1

I have the following code:

<?php
    function get_content($URL)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $URL);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    echo get_content('https://www.example.com');
    ?>

Here I get the content from an external page with get_content from PHP where the html page aswell the css are being loaded.

How can I disable the Css file from this page and use my own stylesheet and is this even possible?

AP2411
  • 11
  • 2
  • 2
    if css is internal yes you can disable it or replace, if this is external then you can provide also an external css by replacing the link of css or the style tag if it's a internal css, you may use DOMParser for this work – Jerson Jan 04 '22 at 11:59
  • Sorry I don't know much about DOM Parser, can you give me an example how to do that please? – AP2411 Jan 04 '22 at 12:04
  • Does [this](https://stackoverflow.com/questions/21988577/replace-all-link-tags-containing-given-href-attribute-with-regex-or-dom) work for you? – vee Jan 04 '22 at 12:07
  • Yes, thank you this method is working! – AP2411 Jan 04 '22 at 12:13

1 Answers1

-2

It is doable but has its catch, you have to manipulate the response you get.

$page = get_content('https://www.example.com');

$pageNewStyle = str_replace('<link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Shared/stacks.css?v=fcfe3af826d7">', '[your style in a public url]', $page);

The Idea is to replace the intedend tag as the style you mentioned, here a put sstatic.net.... as an example

You can check the library guzzle it is excellent to make requests and Crawler(manipulate doom, alter your response)

  • Please share more details. How does the given code remove any CSS? Also, Guzzle is not part of Symfony – Nico Haase Jan 04 '22 at 12:34
  • Sure is not my bad, I edited it, and it removes/replaces the old css with the new one, I just put a new url as an example containing the css extension not js as previos example – Yeison Aristizabal Jan 07 '22 at 13:47