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);
}
}
?>
It is better to do this with sed and awk if you're running Linux
find /home/www/ -type f -exec \ sed -i '' 's/original/replacement/g' {} +