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?
LVL 13
R7AFAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dean OBrienCommented:
I think you only have two options, the first is to edit the php file that is included (which you appear not to be able to do). The only other option is to change it with javascript (you cant access the page elements server side).

Easynow
AielloJCommented:
R7AF:

You should be able to just open the file from php and read it into a string and modify the string.

AielloJ
SwafnilCommented:
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.
<?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;

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

R7AFAuthor Commented:
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.
SwafnilCommented:
I don't know how the PHP module is executed whose content you need, so I am just suggesting what possibilities I can think of, there are other ways that work for sure! If the module is executed somewhere inside your module, you could try to fetch the server's output buffer and modify it's content like in the below example and some references for further reading:

http://us.php.net/manual/en/function.ob-start.php
http://us.php.net/manual/en/function.ob-get-clean.php
http://us.php.net/manual/en/function.ob-flush.php
<?php
// set output buffering to on (should be set before any content is passed to the client)
ob_start();
// ... some other functionality could be executed in between like permission checks etc.

// assume some other output happened before, so we better save this for later use;
//  clear the old buffered content, so we don't mix our content and the module content
$currentContent = ob_get_clean();

// include your module that you don't want or can't change
include ("modules/myunchangablemodule.php");
// call the function that generates the HTML content, the generated content will be stored in the output buffer again
myunchangeablemodule_generate_html();
// fetch the data from the buffer;
// and clear the buffer again, otherwise you could get duplicate content 
$moduleContent = ob_get_clean();

// replace stuff ...
$moduleContent = str_replace("black", "blue", $moduleContent);
// and send the entire output to the client (with the previously saved content)
print $currentContent.$moduleContent;
// send the output to the client
ob_flush();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
R7AFAuthor Commented:
Thank you! That seems to be what I need. I will try it when I'm back at work in a few days.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.