3

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

2
  • 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? Commented Nov 29, 2013 at 18:14
  • yes, please clarify with an example: what would input look like, what should output be Commented Nov 29, 2013 at 18:19

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

Try:

$names = explode(" ", "Andre Filipe da Costa Ferreira");
printf("Welcome %s %s", current($names), end($names));

where current() gets its first element, and end() last one.

Comments

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

Comments

1

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

7 Comments

@ThomWiggers for OP's problem it suits best i have shown him both ways either using index 0 or current()
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.
Why is it better than using [0]?
@ThomWiggers because its php's built-in function
@ThomWiggers i don't want to mess arround with this i have others things to do
|
0

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;
//
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.