Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exploding field on blank spaces problem

Posted on 2013-11-03
4
Medium Priority
?
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 44

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 111

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

719 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