Insert Javascript in file_get_contents() php

Is there a way to insert a javascript at the end of the file_get_contents() ?

I don't mean adding to the output of the function, the javascript. What I want is to put a script at the end of the website I'm getting from the file_get_contents().

Is like using a extension of chrome, which inserts a javascript at the end of the website. But in my case I want the function file_get_contents($url) to output directly everything (the website + javascript inserted).

Is there a way to use cUrl in this situation ?

Is there a way to accomplish this ?

Thanks

Answers


you would need to use *_replace functions on the retrieved code, or use DOM classes to alter the code. There is no way to modify file_get_contents itself to add code

$script =<<<END
   <script type="text/javascript" src="http://www.example.com/mysscript.js"></script>
   </body>
END;
$html = file_get_contents($url);
$html = str_replace("</body>",$script,$html);

This assumes the content being grabbed has full html code, mainly includes the </body> tag


Need Your Help

how to access third party website

php

//$out = fopen($localfilename, 'wb');

Open PDF file by AJAX holding the button

jquery ajax pdf dojo

Can you help me with the following problem?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.