Solved

Exploding field on blank spaces problem

Posted on 2013-11-03
4
279 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 109

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 109

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 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