Solved

Exploding field on blank spaces problem

Posted on 2013-11-03
4
272 Views
Last Modified: 2013-11-03
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

0
Comment
Question by:rcowen00
  • 2
4 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39620493
<?php // RAY_temp_rcowen00.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28284302.html

$value = "LEE ANN MANFIELD";
$name = explode(" ", $value);

// HOW MANY ELEMENTS IN THE ARRAY (INDEX IS FROM ZERO, NOT ONE)
$n = count($name) - 1;

// REMOVE LAST NAME
unset($name[$n]);

// RECONSTRUCT
$new = implode(' ', $name);
echo $new;

Open in new window

Best regards, ~Ray
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 39620555
Alternatively. ...
<?php
$value = "LEE ANN MANFIELD";
$name = explode(" ", $value);
array_pop ( $name );
$newName = implode (" ", $name);

echo $newName;
?>
 http://us1.php.net/array_pop
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39620568
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

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39620585
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now