Gary Gauthier
Gary Gauthier

Reputation: 195

What is the REGEXP php syntax to change LastName, Firstname to FirstName LastName?

I would like to change the order of names from Last, First to First Last. I don't know the REGEXP and the php syntax for it.

Upvotes: 1

Views: 1187

Answers (3)

Jeriko
Jeriko

Reputation: 6637

I hate regular expressions.. You could always do something like

implode(' ',array_reverse(explode(' ,',$string)))

But I'm probably only suggesting that because it's the more intuitive way in Ruby, which I've recently dropped php for :)

Upvotes: 0

user254875486
user254875486

Reputation: 11240

You could just use:

$name = "Lastname, Firstname";
$names = explode(", ", $name);
$name = $names[1] . " " . $names[0];

Upvotes: 4

kennytm
kennytm

Reputation: 523344

return preg_replace('/^([^,]*),\s*(.*)$/', '$2 $1', $theString);

Upvotes: 3

Related Questions