Search code examples
phpstringdelimited

Swap the position of two halves of a string


If I wanted to take a first name / last name string separated by a comma and change the order, how might I go about that?

last name, firstname

should be changed to

firstname last name (without the comma)


Solution

  • This should do it.

       $string = 'last,first';
       list($last,$first) = explode( ",", $string );
       echo $first . ' ' . $last;