tags:

views:

76

answers:

8

I wish to make something that cuts the name to e.g:

Alex Andersson

to uppercase first word after space(first name) "A" like this:

Alex A.

Right now it displays the full name like this:

echo $data["full_name"]; 

Hope you understand what I mean.

A: 

For only two names

preg_replace('/^([A-Z][a-z]+)\s([A-Z])([a-z]+)$/', "\$1 \$2.", $data["full_name"])
Artefacto
A: 
list($fname, $sname) = explode(' ', $data["full_name"]);
$sname = strtoupper(substr($sname, 0, 1));
$new_name = $fname . $sname . '.';
Svisstack
A: 
$nameParts = explode(' ', $data['full_name']);
$lastName = substr(array_pop($nameParts), 0, 1) . '.';
$firstNames = implode(' ', $nameParts);

echo $firstNames . ' ' . $lastName;
Andrei Serdeliuc
I receive Warning: substr() expects parameter 3 to be long, string given in at the lin $lastName
Karem
Sorry, mixed up the parameters, fixed now.
Andrei Serdeliuc
A: 

Untested:

shortened = substr($data["full_name"], 0, strpos($data["full_name"], " ") + 1) + ".";
Bobby
+2  A: 

You described it vaguely, but as I understand that, you can do something like:

$names = explode(" ", $data["full_name"]);
foreach ($names as $key => $name)
{
  if ($key == 0)
    continue;
  $names[$key] = substr($name, 0, 1).'.';
} 
echo implode(" ", $names);

This shortens every name except first one.

Tomasz Struczyński
+1  A: 

You would have to store the first name and last name in different variables, as you can never tell at what position the second name would be.

You can access the first letter of a variable using square brackets.

$data[0];
Liam Spencer
A: 

Simple and clear:

list($forename, $surname) = explode(' ', $data['full_name']);
echo $forename . ' ' . $surname[0] . '.';
Simon
A: 
$name = "Alex Andersson";
$name_arr =  explode(" ", $name);
echo ucfirst($name_arr[0])." ".strtoupper($name_arr[1][0]);
Centurion