This is my second time (in a long time) ever touching php. I am trying to replace the file content between two HTML comments with content from another file located in the same directory.
Right now, I am testing by only replacing the content between two HTML comments with a single line ($newCode).
When I run the following code, however, it wants to replace the entire file with nothing but that $newCode line on each line:
#!/bin/php
<?php
// Testing preg_replace() with string
$tagBegin = '<!-- test4 Begin ColdFusion Template Html_Head -->';
$tagEnd = '<!-- test4 End ColdFusion Template Html_Head -->';
$tagSearch = '/[^'. $tagBegin .'](.*)[^'. $tagEnd .']/';
$strReplace = 'Testing php code';
$testString = '<!-- test4 Begin ColdFusion Template Html_Head -->I should be gone.<!-- test4 End ColdFusion Template Html_Head -->';
// Replaces everything between the two tags with the cfReplace code - THIS WORKS
// echo "Testing string replace...";
// echo preg_replace( $tagSearch, $strRieplace, $testString );
// echo ( "
" .$testString );
// Testing replace on ./testAaron.htm - THIS DOES NOT WORK
echo "
Testing file replace...";
$testFile = 'testAaron.htm';
$newCode = 'Replaced <html> and all Header info!!!'; // to be replaced with cf code
echo preg_replace( $tagSearch, $newCode, file_get_contents( $testFile ) );
?>
I have a feeling it's the file_get_contents()
in the last parameter of the preg_replace()
function, but I don't know why.
When I took out the file_get_contents()
and placed only the $testFile
in it, the script ran with only one line and none of the rest of the testAaron.htm code.
When I opened the testAaron.htm file, there were no changes at all.
I thought maybe 'echo' was just letting me preview and print what would be changed, so I took that out, but it made no difference.