eo92866 Posted February 8, 2011 Share Posted February 8, 2011 trying to create an array that is separating brackets, semi-colon and space from "mydata" and iterating over to create first and last name's separated into two subs... and then loop those through. so i'd like either change the way i can explode and loop the information... or place the first and second subs and skip the third sub in the array. $str = $_POST["mydata"]; $mores = explode("[];",$str); foreach ($mores as $more) { $datas = explode(" ",$more); if (array_values ($datas) === $datas) $xmlBody .= " <member name='$datas[1], $datas[0]' display='$datas[0] $datas[1]'>Name</member>"; } can someone please assist? much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/227087-separate-names-in-loop-with-explode/ Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2011 Share Posted February 8, 2011 Perhaps if you could provide a sample string, someone would be able to help. Quote Link to comment https://forums.phpfreaks.com/topic/227087-separate-names-in-loop-with-explode/#findComment-1171588 Share on other sites More sharing options...
eo92866 Posted February 8, 2011 Author Share Posted February 8, 2011 hope this example of names helps $mydata = Tom Cruise [Tommy]; Hillary Duff [Hills]; Kate Hudson [Katie]; Dave Chappelle [Dave]; Bradley Pitt [brad]; Angelina Jollie [Angie] and the output is into an XML document $xmlBody .= ' <position>' . $_POST["position"] . '</position>'; $str = $_POST["mydata"]; $mores = explode("[];",$str); foreach ($mores as $more) { $datas = explode(" ",$more); if (array_values ($datas) === $datas) $xmlBody .= " <member name='$datas[1], $datas[0]' display='$datas[0] $datas[1]'>Name</member>"; } $xmlBody .= ""; echo $xmlBody; Quote Link to comment https://forums.phpfreaks.com/topic/227087-separate-names-in-loop-with-explode/#findComment-1171596 Share on other sites More sharing options...
Pikachu2000 Posted February 9, 2011 Share Posted February 9, 2011 So the resulting string should look like this, with the bracketed value simply discarded? <member name='Cruise, Tom' display='Tom Cruise'>Name</member> Are they all in exactly the same format, with no surprise middle names, etc.? Quote Link to comment https://forums.phpfreaks.com/topic/227087-separate-names-in-loop-with-explode/#findComment-1171605 Share on other sites More sharing options...
eo92866 Posted February 9, 2011 Author Share Posted February 9, 2011 currently, there are no middle names... and i'm hoping there are no surprise middle names down the line... but i guess i shouldn't discount the possibility of someone deciding to place middle names in there at some point. and yes, they are all in the format of firstName LastName [nickname] or as you put it: <member name='Cruise, Tom' display='Tom Cruise'>Name</member> Quote Link to comment https://forums.phpfreaks.com/topic/227087-separate-names-in-loop-with-explode/#findComment-1171619 Share on other sites More sharing options...
eo92866 Posted February 12, 2011 Author Share Posted February 12, 2011 so this is what i ended up with and did to explode multiple words and then display them separately... and as an added bonus... the example also has a way to place php into the creation of your xml document. $xmlBody .= ' <position>' . $_POST["position"] . '</position>'; //input data assigned to $str $str = $_POST["mydata"]; //splitting $str with ]; and assigning to $mores $mores = explode("];",$str); //creating a loop foreach ($mores as $more) { //splitting data even further with [ and assigning to $datas $datas = explode("[",$more); //trimming array and assigning $datas sub 0 to $name $name = trim($datas[0],""); //trimming array and assigning $datas sub 1 to $nname and used only if needed #$nname = trim($datas[1],""); //split even further on space and assign to $fnameParts $fnameParts = explode(" ",$name); //assign $fnameParts sub 0 to $fname $fname = $fnameParts[0]; //assign $fnameParts sub 1 to $lname $lname = $fnameParts[1]; $xmlBody .= " <member name='$lname, $fname' display='$datas'>Name</member>"; } $xmlBody .= ""; echo $xmlBody; hope this helps someone down the line. Quote Link to comment https://forums.phpfreaks.com/topic/227087-separate-names-in-loop-with-explode/#findComment-1173083 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.