简体   繁体   中英

Php search and replace entire public_html folder inside html files

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); 
 }
?>

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); 
    }
}
?>

如果您正在运行Linux,最好使用sed和awk进行此操作

find /home/www/ -type f -exec \ sed -i '' 's/original/replacement/g' {} +

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM