2

I used file_get_contents function to get content from url and echo it to screen. Here is code.

$data=file_get_contents($_REQUEST['url_name']);
$atIndex = strpos($data,"</head>");
$data = substr_replace($data, '<script type="text/javascript" src="https://tag-test-super-star.c9users.io/server/js/jquery.min.js"></script><script type="text/javascript" src="https://tag-test-super-star.c9users.io/server/js/index.js"></script>', $atIndex, 0);
echo $data;

But if we do this images, styles will not be affect to screen because some urls use local links.

For example like this <img src="img/img.png">

I know if we want to display image correctly we have to add url before img/img.png.

But how can i do it for whole site with php or javascript or something else?

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
JengGe Chao
  • 160
  • 2
  • 19
  • you'd need a full-fledged (possibly headless) web browser to do that. – Calimero Dec 09 '15 at 21:51
  • `we have to add url before img/img.png` - it is not true – Iłya Bursov Dec 09 '15 at 21:53
  • You could try the `base` element; https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base. – chris85 Dec 09 '15 at 21:55
  • Hello, chirs. I want to tell you why do I need to do this. I have to add one javascript link to any urls and display it in any computers. So I didn't use iframe or base tag. The solution is that using php to access url and get content from that and add links and then display it. But problem is that images and styles and also some javascript links doesn't work correctly because I displayed it in my local. Is there no solution to add url to all links? – JengGe Chao Dec 09 '15 at 21:59
  • You could use a parser and update the `href`s and `src`s. – chris85 Dec 09 '15 at 22:04
  • Could you explain me exactly? I get content to string and is there any way to change all hrefs and srcs in string? – JengGe Chao Dec 09 '15 at 22:06
  • Site this [previous question](http://stackoverflow.com/questions/31347340/convert-all-relative-urls-to-absolute-urls-while-maintaining-contents). The answer shows how to make all links within the html use absolute urls. – Steve E. Dec 09 '15 at 22:21
  • See this thread, [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php). I'd try to avoid using a regex for this; or at least on a document level; you drill into the attribute it will be a lot safer – chris85 Dec 09 '15 at 22:22
  • @chris85 Thank you very much. That solved me solve problem. – JengGe Chao Dec 09 '15 at 22:37

0 Answers0