【问题标题】:Replace The Code In A Set Of Files Inside Folder By PHP用PHP替换文件夹内一组文件中的代码
【发布时间】:2014-08-23 23:51:04
【问题描述】:

我的文件夹中有文件(index.html),这些文件包含以下代码

index.html

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
  <script type="text/javascript" src="/wp-content/themes/twentyfourteen/open.js"></script>
 </body>
</html>

我希望脚本 php 寻找此代码

<script type="text/javascript" src="/wp-content/themes/twentyfourteen/open.js"></script>

在文件夹路径内的所有文件中并用此代码替换它

    <script type="text/javascript"
 src="/wp-content/open.js">
</script>

额外信息

文件夹名称:(我的家)

文件夹路径: /我的家/

/myhome/视频/

/我的家/照片/

/myhome/models/

/myhome/联系人/

还有更多...

文件路径: /myhome/index.html

/myhome/video/index.html

/myhome/photo/index.html

/myhome/models/index.html

/myhome/contact/index.html

还有更多...

请帮帮我

【问题讨论】:

  • file_get_contents -> str_replace -> file_put_contents ?
  • 我不明白。你会做php吗?

标签: php html xcode replace directory


【解决方案1】:

mmhh,在 IDE 中打开所有这些文件,然后搜索并替换 \o/

或:

$dir = '/myhome/';

echo 'Looking in '.$dir.' ...<br />';

$scan = scandir($dir);

foreach( $scan as $item ){

    if( is_dir($dir.$item) ){

        echo 'Looking in '.$dir.$item.' ...<br />';

        $scan_bis = scandir($dir.$item);

        if( array_key_exists('index.html', $scan_bis) ){

            $the_file = $dir.$item.'/index.html'; // im not sure if you need the trailing slash or not before index.html

            echo 'Modifying '.$the_file.' ... ';

            $file = file_get_contents($the_file);

            $file = str_replace('/wp-content/themes/twentyfourteen/open.js', '/wp-content/open.js', $file);

            $is_writed = file_put_contents($the_file, $file);

            if( $is_writed === FALSE ){

                echo '( FAILED )<br />';
            }
        }
    }
}

您只需手动修改 /myhome/index.html :)

【讨论】:

  • 感谢您的帮助。没有工作。我在文件夹路径(myhome)中有 200 多个文件(index.html)
  • 哦,你的意思是这段代码不起作用? .. mmhh 好吧,如果你确认我会编辑它。 PS:如果脚本在"/myhome/" 中,请将$dir = "/myhome/"; 更改为$dir = "./";。我想我会编辑这个并给你一个稍微复杂一点的脚本,但无论如何它都会完成工作,明天发布:p
  • 再次感谢脚本在“/myhome/”中,我更改了 $dir = "/myhome/";通过 $dir = "./";我打开 mywebsite.com/myhome/replease.php 浏览器打印:正在寻找 ./ ...正在寻找 ./。 ...查看./.. ...之后我查看index.html,没有任何改变。 replease.php 包含:
  • 我创建了这个应用程序(文本爬虫)。在问我的问题时它的工作原理相同,但在 Windows 系统中。我希望同样的想法适用于我网站中的 PHP。本申请网站:digitalvolcano.co.uk/textcrawler.html
  • 我的代码的输出到底是什么?我没有时间为 ou man soz 开发一个完整的应用程序
【解决方案2】:

这个解决方案 http://pastebin.com/wuWupgBg

改变

if( is_file( $path ) && substr($path, -3)=='php' && substr($path, -17) != 'ChangePHPText.php'){

if( is_file( $path ) && substr($path, -4)=='html' && substr($path, -17) != 'ChangePHPText.php'){

【讨论】:

    猜你喜欢
    • 2018-10-11
    • 2014-12-31
    • 2016-02-04
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多