RE: [PHP] Help and advice sought - search/replace
Thanks for everyone's help. I didn't try this method, but I'll definitely try it next time I do something like this. Here's the final code that I got to work, and it works beautifully. Thanks again for your help on this! The fwrite() function I threw in is fairly useless - it writes the final output to the end of the NAMES.txt file, I suppose this could be modified to write to a new file or whatever you want, but I found it easier to just highlight and copy the output from the browser: ?php $filename = NAMES.txt; $fp = fopen($filename, r+); echo $filename is now open for reading and writing...p; $contents = fread($fp, filesize($filename)); $names = explode(\n, $contents); foreach($names as $name) { $new_name = ereg_replace('([^,]*), ([^ ]*)', '\\2 \\1', $name); echo $new_namebr; fwrite($fp, $new_name.chr(13).chr(10)); } fclose($filename); ? Thanks again, J -Original Message- From: Niklas Lampén [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 26, 2002 10:06 PM To: Php-General Subject: RE: [PHP] Help and advice sought - search/replace If your list looks like this: Salo, Mika Räikkönen, Kimi Häkkinen, Mika And there is no other commas in the list, you can do it something like this: ? $commaSeparatedList = join(\n, file(NAMES.txt)); $tabSeparatedList = preg_replace(, , \t, $commaSeparatedList); $arrayOfTabSepList = explode(\n, $tabSeparatedList); ? I think that'll do. I didn't test it thou.. :) Niklas -Original Message- From: Jason Soza [mailto:[EMAIL PROTECTED]] Sent: 27. kesäkuuta 2002 7:42 To: PHP-General Mailing List Subject: [PHP] Help and advice sought - search/replace 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: ?php $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 $namebr; 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ### This message has been scanned by F-Secure Anti-Virus for Internet Mail. For more information, connect to http://www.F-Secure.com/ ### This message has been scanned by F-Secure Anti-Virus for Internet Mail. For more information, connect to http://www.F-Secure.com/ -- 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] Help and advice sought - search/replace
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: ?php $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 $namebr; 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Help and advice sought - search/replace
If your list looks like this: Salo, Mika Räikkönen, Kimi Häkkinen, Mika And there is no other commas in the list, you can do it something like this: ? $commaSeparatedList = join(\n, file(NAMES.txt)); $tabSeparatedList = preg_replace(, , \t, $commaSeparatedList); $arrayOfTabSepList = explode(\n, $tabSeparatedList); ? I think that'll do. I didn't test it thou.. :) Niklas -Original Message- From: Jason Soza [mailto:[EMAIL PROTECTED]] Sent: 27. kesäkuuta 2002 7:42 To: PHP-General Mailing List Subject: [PHP] Help and advice sought - search/replace 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: ?php $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 $namebr; 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ### This message has been scanned by F-Secure Anti-Virus for Internet Mail. For more information, connect to http://www.F-Secure.com/ ### This message has been scanned by F-Secure Anti-Virus for Internet Mail. For more information, connect to http://www.F-Secure.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php