Page 1 of 1

Need clue. How to process long list of names

Posted: Thu Nov 08, 2007 3:09 pm
by jimandy
Ok, call me lazy (and some of you may) but I'm in a hurry to get what should be a simple script off the ground.

I need to process a long list of text items ---names specifically. What I want is to have a simple form with text box into which I paste a string of names from a single Excel column, and when I hit submit, it will return a page(s) with the modified names ready for me to paste back into my spread sheet. I want it general purpose (may change the conversion function from time to time), but one use would be to convert a column of last names into the metaphone equivalents. I am OK with building the simple form, and I assume I will use the POST method to send the names-string to a php loop reading each into an array to be converted then echoed back to my browser. What I'm not clear on is how to break down the _POST variable into an array. Can I do an explode on it using \n as a separator?

As usual, thanks for your help.

Posted: Thu Nov 08, 2007 3:23 pm
by VladSun

Code: Select all

$arrayOfNames = explode('\n', $namesString)
?

Posted: Thu Nov 08, 2007 9:07 pm
by jimandy
Thanks, VladSun. I needed confirmation on that approach.

Posted: Thu Nov 08, 2007 10:05 pm
by jimandy
oops, doesn't seem to work...

Code: Select all

<?
$names =
"Jim
Sam
Tom";
$namesarray= explode('\n', $names);
echo $namesarray[0];
?>
Shouldn't that just echo "Jim"? Each name in $Names is on a new line. But I get
"Jim Sam Tom" and an error echoing $namesarray[1]

I also tried $names= $_POST['names'];
...sent it a names list, each on a new line from an html form and got the same result.

Posted: Thu Nov 08, 2007 10:54 pm
by Zoxive
Use var_dump/print_r to view the arrays.

Try with double quotes.

Code: Select all

$array = explode("\n",$names);
print '<pre>'; print_r ($array); print '</pre>';
// You can also use commas or anything you would like spaces maybe instead of Line breaks(\n)

Posted: Fri Nov 09, 2007 7:00 am
by VladSun
Zoxive wrote:Try with double quotes.

Code: Select all

$array = explode("\n",$names);
Zoxive is right - my mistake.

Posted: Fri Nov 09, 2007 10:44 am
by jimandy
That did it! Thanks Zoxive and VladSun

jimandy