Jump to content

exploding a text area


freelance84

Recommended Posts

Hi,

 

I've been asked to modify a section on a site for a local club.

 

Basically, I've been asked to improve the name input section (a user may have to input up 15 names in one visit to the site).

 

At the moment the site works like this:

The user enters one members first name in one form box, then the surname in another, then presses "add name" which adds it to the list.

 

What i want to be able to do (and i'm sure i've seen it somewhere), is have the user enter the list of names into one big text area: Type in the first name then the surname, hit enter for a new line in the text area then type the next name....etc. When the list is complete press "add names" button.

 

Is there a name for this type of input? And does it work on the following principle:

1. The entire text sent from the text area can be exploded into an array using the line break as the delimiter

2. Each value in the array is then the first name and second name.

3. I can then explode each value further now using the space as the delimiter.

4. I am left with a bunch of arrays containing the first name in one value and the surname in the second

 

Any help or pointers here would be class

 

Cheers,

 

John

 

 

 

Link to comment
Share on other sites

I would do just as you say with the text area and script it something like.

 

 

$name = $_POST['textarea'];$seperated_names = explode("\n",$name);$i = 0;foreach($seperated_names as $person) {$break_names = explode(' ',$person);$people_array[++$i]['firstname'] = $break_names[0];$people_array[$i]['lastname'] = $break_names[1];}foreach($people_array as $people) {echo '<br /><br />Firstname: ' . $people['firstname'] . '<br />Lastname: ' . $people['lastname'];}

 

Link to comment
Share on other sites

I can see the issue with adding middle names into the equation, however for now i am going to stick with only entering in the "known as" forename and the surname:

 

$sent_names = htmlentities($_POST['names']);
$seperated_names = explode("\n",$sent_names);
$i = 0;
foreach($seperated_names as $person)
{
	$break_names = explode(' ',$person);
	$first_name = ucfirst($break_names[0]);
	$surname = ucfirst($break_names[1]);
	if(empty($first_name) or empty($surname))
		{
			$no_name[++$i]['firstname'] = $first_name;
			$no_name[$i]['lastname'] = $surname;
		}
	else
		{
			$people_array[++$i]['firstname'] = $first_name;
			$people_array[$i]['lastname'] = $surname;
		}
}
print_r($people_array);
echo "<br/>There were some faults<br/>";
print_r($no_name);
echo "<br/>";

 

Hmm. The above works... if the users sticks to the rules.

 

-However, a double space between the first and last name results in the last name being empty.

-A space before the first name results in the first name being empty and the first name being the surname.

 

To get rid of the spaces at the start i could run a while loop saying if the 1st character == " " replace with ""

 

I would do the same to rid spaces at the end.

 

After checking for spaces at the beginning and end i want to be able to check the number of spaces between the two names... this is where i'm stumped. I want to replace multiple spaces with one space.

 

I know if i explode the string after ridding the spaces at the start and end means that the first and last value in the explode will be the first and last name. However i want to do it the other way as this will allow me to expand the system later to take in middle names.

 

Any ideas??

Link to comment
Share on other sites

preg_replace() would be better in this instance, since the pattern is actually "2 or more" spaces. Then you can process it further.

 

$name = ' billy  bob                                                       thornton ';

$name2 = preg_replace( '~\s{2,}~', ' ', $name);
print_r(explode(' ', trim($name2)));
// returns Array ( [0] => billy [1] => bob [2] => thornton )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.