使用php将跟踪代码添加到CSV文件的每一行中的第一个链接实例

使用php将跟踪代码添加到CSV文件的每一行中的第一个链接实例

问题描述:

I would like to

  1. download a csv file from a webserver
  2. add a tracking code to the first instance of a link in each line of the csv file.
  3. upload the file to a ftp server

    How could I do that using php?

The important part is no.2 as I have difficulties figuring out the functions and regular expressions to use for modifying the links. Downloading and uploading the file I can figure out myself.

example

input:

ID, text1, link1, text2, text3, link2
1234, something, http://www.example.com/a/b/c, "lorem", "ipsum", http://www.example.com/image.gif
1235, something, http://www.example.com/dddd, "lorem", "ipsum", http://www.example.com/image.gif
1236, something, http://www.example.com/e/f/g/h, "lorem", "ipsum", http://www.example.com/image.gif

output:

ID, text1, link1, text2, text3, link2
1234, something, http://www.example.com/a/b/c?tracking_code=1&tr2=2, "lorem", "ipsum", http://www.example.com/image.gif
1235, something, http://www.example.com/dddd?tracking_code=1&tr2=2, "lorem", "ipsum", http://www.example.com/image.gif
1236, something, http://www.example.com/e/f/g/h?tracking_code=1&tr2=2, "lorem", "ipsum", http://www.example.com/image.gif

lets assume your getting each line using fgetcsv

$line = fgetcsv($fp);
$line[2] = $line[2] . "?tracking_code=1&tr2=2";
$data[] = $line;

......

foreach ($data as $line) {
    fputcsv($fp, $line);
}

The code i posted is abstract and assumes your familiar with how to iterate through a csv file.