Jump to content

separate names in loop with explode


eo92866

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

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.

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.