Solved

Exploding field on blank spaces problem

Posted on 2013-11-03
4
300 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 110

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 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

627 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