Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm still a freshman on PHP. So i have a variable called 'full name' and i was trying to explode and implode the first and the last variable value.

$fullname='Andre Filipe da Costa Ferreira';
$namepieces=explode('', $fullname);
$flname=implode('', namepieces[0], namepieces[lastvar]);
echo "Welcome".$flname;

I would appreciate if someone could help me! Thanks :D

share|improve this question
    
You're missing the $ in front of namepieces on line 3; otherwise - what is the issue that you're having? Do you get an error message? A blank screen? The wrong data? –  andrewsi Nov 29 '13 at 18:14
    
yes, please clarify with an example: what would input look like, what should output be –  hanzo2001 Nov 29 '13 at 18:19

4 Answers 4

up vote 1 down vote accepted

This with name pieces separated with a space, and works with a single name piece like Andre:

<?php
$fullname = 'Andre Filipe da Costa Ferreira';
$namepieces = explode(' ', $fullname);
$n = count($namepieces);
if($n > 1) {
  $flname = implode(' ', array($namepieces[0], $namepieces[$n-1]));
} else {
  $flname = $namepieces[0];
}
echo "Welcome " . $flname;
//
?>

This gets:

Welcome Andre Ferreira
share|improve this answer

You need to use end($namepieces); which returns the value of the last element or FALSE for empty array.Also your are missing $ before namepieces

$flname=implode('', $namepieces[0], end($namepieces));

end()

Another example taken from php.net for getting first and last element from array

$items = array( 'one', 'two', 'three' );
$lastItem = end( $items ); // three
$current = current( $items ); // one
share|improve this answer
    
using current probably is tricky when used around iterators. –  Thom Wiggers Nov 29 '13 at 18:22
2  
@ThomWiggers for OP's problem it suits best i have shown him both ways either using index 0 or current() –  M Khalid Junaid Nov 29 '13 at 18:24
    
current() isn't a way to get index 0: current() is a way to get the current element of an array when walking through it using an iterator/next()/prev(). That it happens to be index 0 at first isn't a reason to use current() for this. –  Thom Wiggers Nov 29 '13 at 18:30
1  
@ThomWiggers because its php's built-in function –  M Khalid Junaid Nov 29 '13 at 18:41
2  
@ThomWiggers i don't want to mess arround with this i have others things to do –  M Khalid Junaid Nov 29 '13 at 18:59
<?php
$fullname = 'Andre Filipe da Costa Ferreira';
$namepieces = explode(' ', $fullname);
$flname = implode(' ', array($namepieces[0], $namepieces[count($namepieces)-1]));
echo "Welcome " . $flname;
//
?>
share|improve this answer

Its beter way I guess

<?php
$fullname = 'Andre Filipe da Costa Ferreira';
$namepieces = explode(' ', $fullname);
$n = count($namepieces);
$n > 0 ? $flname = implode(' ', array($namepieces[0], $namepieces[$n-1])) : $flname = $namepieces[0];
echo "Welcome " . $flname;
//
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.