Need clue. How to process long list of names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Need clue. How to process long list of names

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

$arrayOfNames = explode('\n', $namesString)
?
There are 10 types of people in this world, those who understand binary and those who don't
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Post by jimandy »

Thanks, VladSun. I needed confirmation on that approach.
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Zoxive wrote:Try with double quotes.

Code: Select all

$array = explode("\n",$names);
Zoxive is right - my mistake.
There are 10 types of people in this world, those who understand binary and those who don't
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Post by jimandy »

That did it! Thanks Zoxive and VladSun

jimandy
Post Reply