3

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

6
  • 2
    Can you provide code you have written? Its best to show that you have made an attempt before posting here. Commented Mar 12, 2015 at 14:17
  • I've added the code I've already written Commented Mar 12, 2015 at 14:37
  • Are these string prefixes constant? For example, StreamHandler.ashx?SubscriptionID=? Commented Mar 12, 2015 at 15:18
  • preg_match or parse_str \ parse_url for finding a pattern. fwrite, file_put_contents for writing to a file Commented Mar 12, 2015 at 15:19
  • StreamHandler.ashx?SubscriptionID= is always the same what changes is just the ID afterwards Commented Mar 12, 2015 at 15:36

1 Answer 1

1

You can do a preg_replace_callback to find the string you want to search, then look the file extension up, and replace.

  • Find matches to replace
  • See if the file exists by looking for the filename
  • If it exists, replace string
  • If it doesn't exist, keep current string

$csv = preg_replace_callback('/StreamHandler\.ashx\?SubscriptionID=([0-9]+)/', function($matches) {
   $file = $matches[1]; //get the filename
   $file = glob($file .".*"); //find the file in the fileserver (in the current directory)

   if( isset($file[0]) ) { //was there a match?
      return 'http://newdomain.com/images/'. $file[0]; //replace
   }

   return $matches[0]; //dont replace because file doesnt exist
}, $csv);
Example
  • I have the file 6348.png.
  • My csv file holds: StreamHandler.ashx?SubscriptionID=6348,StreamHandler.ashx?SubscriptionID=6349,StreamHandler.ashx?SubscriptionID=635,StreamHandler.ashx?SubscriptionID=64

The output:

http://newdomain.com/images/6348.png,StreamHandler.ashx?SubscriptionID=6349,StreamHandler.ashx?SubscriptionID=635,StreamHandler.ashx?SubscriptionID=64
Sign up to request clarification or add additional context in comments.

6 Comments

The output should be only newdomain.com/images/6348.png. Will this script replace only /StreamHandler\.ashx\?SubscriptionID=3232 with the new url in the csv file?
Yes, it will only find a string like StreamHandler.ashx?SubscriptionID=6349 and replace just that string with the new string http://newdomain.com/images/6349.png
Ok and how do I make it loop through all the csv file rows?
So I managed to loop through the whole document but now it needs to save the new values to the csv file, any help here?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.