Re: [PHP] Re: Help and advice sought - search/replace

2002-07-02 Thread Richard Lynch

>> Just looking for a pointer or functions I can use to do this...
>> 
>> I have a spreadsheet with a couple thousand entries that I'll be using for
>> populating a MySQL database. The spreadsheet has names listed in "last,
>> first" - the database I'll be populating has separate fields for first and
>> last names.
>> 
>> I exported the names column from the spreadsheet in tab-delimited format
>and
>> I figured I could come up with a quick PHP script that would open the text
>> file, find each instance of ", ", take all that's before that delimiter
>and
>> move it after all that's after that delimiter, inserting a tab in between.
>> So "last, first" would become "first[tab]last".
>> 
>> So far, I've gotten:
>> >  $filename = "NAMES.txt"; //name the file...
>>  $fp = fopen($filename,"r+"); //open it...
>>  echo "Reading $filename...";
>> 
>>  $contents = fread($filename, filesize($filename)); //read it...
>> 
>>  $names = explode("\n", $contents);
>> 
>>  foreach($names as $name);
>>  echo "$name";
>> 
>>  fclose($filename);
>> ?>

$file = file("NAMES.txt") or die("Could not open file");
$output = fopen("NAMES.out") or die("Could not open NAMES.out -- Do 'touch
NAMES.out; chmod 666 NAMES.out'  REMOVE NAMES.out when DONE!!!");
while(list(,$line) = each($file)){
  $fields = explode("\t", $fields);
  $name = $fields[3]; # 3 is probably wrong... Which field is last, first?
  $parts = explode(',', $name);
  $last = trim($parts[0]);
  $first = trim($parts[1]);
  $fields[3] = $last; # Again, 3 is the old name field.
  $fields[] = $first; # Take first name on to the end.
  $newline = implode("\t", $fields);
  $wrote = fputs($output, $newline);
  if ($wrote != strlen($newline)){
echo "Only wrote $wrote of ", strlen($newline), " characters!\n";
}

-- 
Like Music?  http://l-i-e.com/artists.htm


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Re: Help and advice sought - search/replace

2002-07-02 Thread Jarrad Kabral

http://www.php.net/manual/en/function.fgetcsv.php

that will help you  :)



-Original Message-
From: David Robley [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, 3 July 2002 11:49 AM
To: [EMAIL PROTECTED]
Subject: [PHP] Re: Help and advice sought - search/replace


In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] 
says...
> Just looking for a pointer or functions I can use to do this...
> 
> I have a spreadsheet with a couple thousand entries that I'll be using for
> populating a MySQL database. The spreadsheet has names listed in "last,
> first" - the database I'll be populating has separate fields for first and
> last names.
> 
> I exported the names column from the spreadsheet in tab-delimited format
and
> I figured I could come up with a quick PHP script that would open the text
> file, find each instance of ", ", take all that's before that delimiter
and
> move it after all that's after that delimiter, inserting a tab in between.
> So "last, first" would become "first[tab]last".
> 
> So far, I've gotten:
>$filename = "NAMES.txt"; //name the file...
>   $fp = fopen($filename,"r+"); //open it...
>   echo "Reading $filename...";
> 
>   $contents = fread($filename, filesize($filename)); //read it...
> 
>   $names = explode("\n", $contents);
> 
>   foreach($names as $name);
>   echo "$name";
> 
>   fclose($filename);
> ?>
> 
> Obviously, I've written in some stuff just so I can see what I'm doing,
the
> echo in the foreach for example. But I'm stuck on what to use to actually
> separate and rewrite each $name. I'm assuming I'll have to create a
while()
> or for() loop and use a regular expression for this? I'm not sure where to
> look. Any help would be great - just so I won't have to go in and manually
> separate first and last names. Thanks!
> 
> Jason Soza


Off the top of my head (pseudocode):

Read the file into an array with file()
 foreach element
  explode on tab
  select name field
  explode on comma
  extract last, first fields and write to...
done

Cheers
-- 
David Robley
Temporary Kiwi!

Quod subigo farinam

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re: Help and advice sought - search/replace

2002-07-02 Thread David Robley

In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] 
says...
> Just looking for a pointer or functions I can use to do this...
> 
> I have a spreadsheet with a couple thousand entries that I'll be using for
> populating a MySQL database. The spreadsheet has names listed in "last,
> first" - the database I'll be populating has separate fields for first and
> last names.
> 
> I exported the names column from the spreadsheet in tab-delimited format and
> I figured I could come up with a quick PHP script that would open the text
> file, find each instance of ", ", take all that's before that delimiter and
> move it after all that's after that delimiter, inserting a tab in between.
> So "last, first" would become "first[tab]last".
> 
> So far, I've gotten:
>$filename = "NAMES.txt"; //name the file...
>   $fp = fopen($filename,"r+"); //open it...
>   echo "Reading $filename...";
> 
>   $contents = fread($filename, filesize($filename)); //read it...
> 
>   $names = explode("\n", $contents);
> 
>   foreach($names as $name);
>   echo "$name";
> 
>   fclose($filename);
> ?>
> 
> Obviously, I've written in some stuff just so I can see what I'm doing, the
> echo in the foreach for example. But I'm stuck on what to use to actually
> separate and rewrite each $name. I'm assuming I'll have to create a while()
> or for() loop and use a regular expression for this? I'm not sure where to
> look. Any help would be great - just so I won't have to go in and manually
> separate first and last names. Thanks!
> 
> Jason Soza


Off the top of my head (pseudocode):

Read the file into an array with file()
 foreach element
  explode on tab
  select name field
  explode on comma
  extract last, first fields and write to...
done

Cheers
-- 
David Robley
Temporary Kiwi!

Quod subigo farinam

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php