2015-11-13 20:32
浏览 51


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:

        // 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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushaqing7080
    doushaqing7080 2015-11-13 20:37

    Your RegEx is definitely incorrect. Look what it evaluates to:

    /[^<!-- test4 Begin ColdFusion Template Html_Head -->](.*)[^<!-- test4 End ColdFusion Template Html_Head -->]/

    This is wrong; brackets in RegEx denote a character set, not a literal string. Furthermore, adding the caret ^ symbol negates the character set, meaning essentially "none of these characters".

    If you want to search for a literal, just use those characters:

    $tagSearch = '/'. $tagBegin .'(.*)'. $tagEnd .'/';

    Also, I would make the wildcard lazy by adding a ? so it doesn't potentially match other tags in your code:

    $tagSearch = '/'. $tagBegin .'(.*?)'. $tagEnd .'/';

    Finally, it sounds like you're trying to actually modify the file itself. To do that, you'll need to write your modified data back to the file. Changing the data in-memory will not automatically save those changes to the file on disk.

    点赞 评论
  • dsp15140275697
    dsp15140275697 2015-11-13 21:01

    try this function

    echo replace_between($tagSearch, $tagBegin, $tagEnd, file_get_contents( $testFile ));
    function replace_between($str, $needle_start, $needle_end, $replacement) {
        $pos = strpos($str, $needle_start);
        $start = $pos === false ? 0 : $pos + strlen($needle_start);
        $pos = strpos($str, $needle_end, $start);
        $end = $pos === false ? strlen($str) : $pos;
        return substr_replace($str, $replacement, $start, $end - $start);
    点赞 评论