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
https://forums.phpfreaks.com/topic/227087-separate-names-in-loop-with-explode/
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;

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.?

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>

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.