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条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀