如何从另一台服务器获取一个html页面,并在该文件中的某个点插入php代码

We're completely redoing our website, and putting most of our pages into a new CMS (Adobe cq5- on server

1). However, we have some php/mysql pages that cant go in there, so we are housing them on our own server(server2). I need our pages on server2 to look exactly like the page on server1.

There is a template on Server 1, with a url of: http://staging-cms.com/content/directory/template.html

So, I need to
1.pull the template from Server1 using the above URL. 2.Look in that template for this div:

<div class="text"><p>CONTENT FOR TEMPLATE GOES HERE.<br></p></div>

3.Take the content of the page on server 2 and insert it at the above point. 4. Return the full page, with template and contents onto a page on server2.

So, the final url would be something like: http://server2.com/books.php

Is this possible?

I've tried this below, but all I get is a page with databases.php printed.

$Content = file_get_contents("http://staging-cms.uc.edu/content/libraries   /template.html");
$Content = str_ireplace('CONTENT FOR TEMPLATE GOES HERE','databases.php','What goes here??');//staging-cms.uc.edu/content/libraries/template.html);
print ($Content);`

I've tried other things where I can return the template (kind of). But I cant seem to get it all working together?

$template = file_get_contents("http://staging-cms.uc.edu/content/libraries/template.html");
list($top, $bottom) = explode('CONTENT FOR TEMPLATE GOES HERE', $template);

echo $top;
include '/path/to/databases.php';
echo $bottom;

The PHP code in databases must actually be executed before displaying to the user, so you can't use str_ireplace(), this deals with literal strings only, not file names or PHP code.

Instead you need to split the template into 2 parts (top and bottom), and then include the PHP code which will be executed.

I highly recommmend explode(); function. If you have have the exact text "CONTENT FOR TEMPLATE GOES HERE." your code will be something like this.

<?php 
$Content = file_get_contents("http://template.url");
$PHPFileContent = file_get_contents("/path/to/databases.php");
$explodedContent = explode("CONTENT FOR TEMPLATE GOES HERE", $Content);

$theFinalContent = "$explodedContent[0] $PHPFileContent $explodedContent[1]";
?>

You need to search how explode() works btw.

Hope helps.

One Good Design Patters that i like to use is that:

$new_file = file_get_contents($file_for_future);

$old_file = file_get_contents($file_to_copy_body);

preg_match('\preg to find the right position\', $new_file,$match, PREG_OFFSET_CAPTURE);

reg pattern can be '\div class=\"text\">

\' but is untested, do it yourself.

$pos = $match[0][1];

The third and forth command is for find the right position that you want to put your new code inside the new file, is a INT number and should be the exact point that you want to start writing your old code.

PREG_OFFSET_CAPTURE in the preg_match force the regular expression to say where they find the standart.

And the magic is:

$final_file = substr($new_file, 0, $pos)." ".$old_file." ".substr($new_file, $pos);

This logic will generate a file, using the new template, inserting the old code right where you want to put it.