开发者

How do i call the css for the page that i get from file_get_content

I am using the following code

<div id="content"> <?php
$homepage = file_get_contents("www.yahoo.com");
echo $homepage;
?></div>

The page appears fine but its without the style sheet. How do i call those style sheets ?

and the link which appears are not Valid (it appears through my domain ) How do i fix it ?

The code that i was using for iFrame which is not working is below

 <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript"> 

$(document).ready(function(){
  $("button").click(function(){


    var iframe = document.getElementById("frm");


        selection = getIframeSelectionText(iframe); 
        alert(selection);
        if(selection.length >= 3) 
        { el = $('body', $('iframe').contents());
        el.html(el.html().replace(selection, "<span class='highlight'>" + selection + "</span>"));
        }

  })开发者_JAVA百科;
    });




function getIframeSelectionText(iframe) {
var win = iframe.contentWindow;
var doc = win.document;

if (win.getSelection) {  
return win.getSelection().toString();

} else if (doc.selection && doc.selection.createRange) {
return doc.selection.createRange().text;

}
}

    </script>


If stylesheet URLs in the page are absolute, everything should display correctly. Otherwise you will have to modify their URLs in the page code (either str_replace or regular expressions).

Also, you can expect at least some styles of the page to break, since you are embedding the whole HTML in an "alien" HTML structure. Generally, HTML tag should always be the out-most tag in the page.

I doubt you will come up with solution which works flawlessly for all websites. Why don't you use IFRAME instead?


This isn't a perfect approach, but you could make use of the <base> tag to change relative URLs so that they match against the domain you're fetching from; that should instruct the browser to load relative URLs.

<div id="content">
<base href="http://www.scraped-domain.com/" />` 
<?php
$homepage = file_get_contents("http://www.scraped-domain.com/");
echo $homepage;
?></div>
<base href="http://your-domain.com" /> <!--reset after outputting the contents, so that your page resumes treating your relative URLs to your domain. -->

(Untested.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜