dqmhgz5848 2018-08-07 16:16
浏览 47
已采纳

Php搜索并替换html文件中的整个public_html文件夹

With the following script we are able to find a copy inside a file that has the string "new google.maps.LatLng" the lat long value and paste it inside the same file value of the Openstreetmap code where it finds "locLat, locLng". The script works properly, but it has to be done one file at the time. Can someone help me to do it for an entire folder (for example public_html folder)?

<?php 

$file = 'example.html';
$searchfor = 'new google.maps.LatLng';
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";

if(preg_match_all($pattern, $contents, $matches)){
//    echo "Found matches:<br />";
//echo implode("<br />", $matches[0]);
$match = implode("<br />", $matches[0]);
$tmpArr = explode('(', $match);
$tmpArr = explode(')', $tmpArr[1]);
$tmpArr = explode(',', $tmpArr[0]);
//print_r($tmpArr);die();
$contents = str_replace("var mymap = L.map('mapid').setView([locLat, locLng], 18);","var mymap = L.map('mapid').setView([".$tmpArr[0].", ".$tmpArr[1]."], 18);",$contents);
$contents = str_replace("var marker = L.marker([palermo],","var marker = L.marker([".$tmpArr[0].", ".$tmpArr[1]."],",$contents);
$contents = str_replace("var marker = L.marker([locLat, locLng],","var marker = L.marker([".$tmpArr[0].", ".$tmpArr[1]."],",$contents);
echo file_put_contents($file,$contents);
}

else{
     echo "No matches found";
fclose ($file); 
 }
?>
  • 写回答

2条回答 默认 最新

  • dsq2015 2018-08-07 17:23
    关注

    Try this

    <?php 
    $dir = '/path/to/folder';
    
    if (!file_exists($dir)) throw new Exception("Folder not found", 1);
    
    $files = scandir($dir);
    // or following for specific file type
    // $files = preg_grep('/.*\.html/', scandir($dir));
    
    foreach ($files as $file) {
        $file = $dir . '/' . $file;
        $searchfor = 'new google.maps.LatLng';
        $contents = file_get_contents($file);
        $pattern = preg_quote($searchfor, '/');
        $pattern = "/^.*$pattern.*\$/m";
    
        if(preg_match_all($pattern, $contents, $matches)){
    //    echo "Found matches:<br />";
    //echo implode("<br />", $matches[0]);
            $match = implode("<br />", $matches[0]);
            $tmpArr = explode('(', $match);
            $tmpArr = explode(')', $tmpArr[1]);
            $tmpArr = explode(',', $tmpArr[0]);
    //print_r($tmpArr);die();
            $contents = str_replace("var mymap = L.map('mapid').setView([locLat, locLng], 18);","var mymap = L.map('mapid').setView([".$tmpArr[0].", ".$tmpArr[1]."], 18);",$contents);
            $contents = str_replace("var marker = L.marker([palermo],","var marker = L.marker([".$tmpArr[0].", ".$tmpArr[1]."],",$contents);
            $contents = str_replace("var marker = L.marker([locLat, locLng],","var marker = L.marker([".$tmpArr[0].", ".$tmpArr[1]."],",$contents);
            echo file_put_contents($file,$contents);
        }
    
        else{
            echo "No matches found";
            fclose ($file); 
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线