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

报告相同问题?