freelance84 Posted October 9, 2010 Share Posted October 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/ Share on other sites More sharing options...
jcbones Posted October 9, 2010 Share Posted October 9, 2010 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'];} Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120554 Share on other sites More sharing options...
freelance84 Posted October 9, 2010 Author Share Posted October 9, 2010 Cool, cheers i thought i mmight on the right track Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120558 Share on other sites More sharing options...
Pikachu2000 Posted October 9, 2010 Share Posted October 9, 2010 A name like 'John Paul Jones' will will result in Firstname: John Lastname: Paul Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120560 Share on other sites More sharing options...
PaulRyan Posted October 9, 2010 Share Posted October 9, 2010 To combat that dilema change the following... $seperated_names = explode("\n",$name); To... $seperated_names = explode("\n",$name,2); Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120563 Share on other sites More sharing options...
freelance84 Posted October 10, 2010 Author Share Posted October 10, 2010 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?? Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120829 Share on other sites More sharing options...
BlueSkyIS Posted October 10, 2010 Share Posted October 10, 2010 to get rid of spaces at the start and end of a string, you can use trim() Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120830 Share on other sites More sharing options...
freelance84 Posted October 10, 2010 Author Share Posted October 10, 2010 ah cool cheers. Just the middle spaces to figure out now Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120832 Share on other sites More sharing options...
litebearer Posted October 10, 2010 Share Posted October 10, 2010 use string replace where the needle is 2 spaces and the replacement is one space Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120833 Share on other sites More sharing options...
freelance84 Posted October 10, 2010 Author Share Posted October 10, 2010 Yea but then i have to write another line for three spaces, and another for four...ect. I suppose i could leave the middle spaces. explode the string then array push the non empty items from the explode array. Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120835 Share on other sites More sharing options...
Pikachu2000 Posted October 10, 2010 Share Posted October 10, 2010 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120839 Share on other sites More sharing options...
freelance84 Posted October 10, 2010 Author Share Posted October 10, 2010 Ah, cool cheers. Not very good with preg_replace: Don't really get how to figure out the patterns bit yet Quote Link to comment https://forums.phpfreaks.com/topic/215493-exploding-a-text-area/#findComment-1120852 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.