duandanxiu6965 2015-11-13 20:32
浏览 54
已采纳

如何在两个HTML注释之间获取文件内容并将其替换为另一个文件中的内容?

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.

  • 写回答

2条回答 默认 最新

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器