I have a csv file with multiple columns, in some of these columns there are some HTML tags that looks like StreamHandler.ashx?SubscriptionID=6348
. Then there is a folder that contains all images renamed with ID.Extension i.e. 6348.jpg.
I would like to create a script that searches for StreamHandler.ashx?SubscriptionID=6348
and replaces it with http://newdomain.com/images/6348.jpg
.
Note that not all the files are .jpg so the extension needs to be checked when the file is found.
$file = fopen("images.csv", "r");
$lines = array();
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
$lines[] = $line;
}
foreach ($lines as $line => $data) {
$data = preg_replace_callback('/StreamHandler\.ashx\?SubscriptionID=([0-9]+)/', function($matches) {
$img = $matches[1]; //get the filename
$img = glob("/Users/sandro/Sites/test/destination/" . $img . ".*"); //find the file in the fileserver (in the current directory)
$img[0] = str_replace ( "/Users/sandro/Sites/test/destination/", 'http://newdomain.com/images/', $img[0] );
if( isset($img[0]) ) { //was there a match?
return $img[0]; //replace
}
return $matches[0]; //dont replace because file doesnt exist
}, $data);
print_r($data);
}
fclose($file);
I've written the part to open and read the csv file but the search is still missing. Any thoughts?
Thanks