R7AF
asked on
Catch content of include in string
I have the following issue. I need to include a module in a page, and the module is not customizable. Well guess, it needs to be customized to a different layout. The module creates HTML, and I want to replace some strings in that HTML. I could do this clientside with Javascript, but I prefer to do this in PHP if it's possible.
How can I do this?
How can I do this?
R7AF:
You should be able to just open the file from php and read it into a string and modify the string.
AielloJ
You should be able to just open the file from php and read it into a string and modify the string.
AielloJ
You first have to read the HTML code after the module executed the script which will require reading the file via URL; after that you can change what you need to change. Directly including the file through include() or require() will not work because you won't get the generated HTML code (which is what you need) but the PHP script code itself.
Please note that if you use the below method, the user information from the current session will not be available when reading a file remotely because the call is issued from the server and not by the user; this means that i.e. cookies or sessions will not be passed along. If you are using session variables and pass the SESSIONID on each call, you can pass this in the file_get_contents() call to allow accessing session variables.
Please note that if you use the below method, the user information from the current session will not be available when reading a file remotely because the call is issued from the server and not by the user; this means that i.e. cookies or sessions will not be passed along. If you are using session variables and pass the SESSIONID on each call, you can pass this in the file_get_contents() call to allow accessing session variables.
<?php
// requires allow_url_fopen=On in php.ini, otherwise you can't read remote URIs
$content = file_get_contents("http://www.myserver.de/?module=yourmodule&yourparm1=value1&yourparm2=value");
// change whatever needs to be changed
$content = str_replace("style/style.css", "style/myownstyle.css", $content);
// output the code
print $content;
ASKER
So in effect it is not possible to cath the HTML on the server before it is sent to the client (which can be the server itself), and the closest to getting this is Swafnil's solution.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! That seems to be what I need. I will try it when I'm back at work in a few days.
Easynow