Link to home
Start Free TrialLog in
Avatar of rcowen00
rcowen00Flag for United States of America

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];}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Beverley Portlock
Alternatively. ...
<?php
$value = "LEE ANN MANFIELD";
$name = explode(" ", $value);
array_pop ( $name );
$newName = implode (" ", $name);

echo $newName;
?>
 http://us1.php.net/array_pop
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);

Open in new window

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