rcowen00
asked on
Exploding field on blank spaces problem
I have the following code that I am trying to extract the first name or first name and middle name (if given). I'm expecting it to return "LEE ANN" but it returns just "LEE". What am I doing wrong? Thanks!
<?php
$value = "LEE ANN MANFIELD";
$name = explode(" ", $value);
for($i = 0; $i < count($name); $i++){}
if (count($i)== 2 ) {
echo $name[0] & " " & $name[1] ;}
else {echo $name[0];}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The explode() function takes a limit argument, and if this is negative it will miss out a number of elements from the end of the array, so the simplest ways is:
$value = "LEE ANN MANFIELD";
$name = explode(" ", $value, -1); //drop the last element
echo implode(" ", $name);
Thanks for the points and thanks for using EE. As usual at EE: A variety of good solutions from a number of well-qualified experts! ~Ray
<?php
$value = "LEE ANN MANFIELD";
$name = explode(" ", $value);
array_pop ( $name );
$newName = implode (" ", $name);
echo $newName;
?>
http://us1.php.net/array_pop