Jump to content

Making "John A. Smith" Display: Smith, John A.


JSHINER

Recommended Posts

I currently use the following to pull last name:

 

SELECT"vendors.name, SUBSTRING_INDEX(vendors.name, ' ', -1) AS lastname FROM table

 

I did this so I could sort by last name.

 

But now I want it to display last name FIRST, then the rest.

 

So I would like to create another variable... firstname - so I can display John A. Smith as Smith, John A.

 

How can I do this ?

Link to comment
https://forums.phpfreaks.com/topic/50370-making-john-a-smith-display-smith-john-a/
Share on other sites

That uses the . as the end point. I need to use spaces. I need the last space John A _ Smith as the divider. Everything before = first name, everything after = last name. The code I posted uses everything after the last space as last name. I need something similar for first name.

 

The reason is some names are "Mike James Smith" And do not use middle initials, but full names.

huh?

<?php

$string = "John A. Smith";

$explode = explode(".",$string);

echo $explode[1] . ", " . $explode[0];

//produces Smith, John A

?>

Its almost impossible to make something to work with every kind of name. I suggest you try to make sure all names are entered in the same way.

if the name for the vendor looks like this John A Smith or John Allen Smith you could always do this

 

$names = explode(" ", $lastname);
$firstname = $names[0];
$middlename = $names[1];
$lastname = $names[2];

 

This will break up the name into parts separated by spaces.

 

Ray

 

 

This should give you what you're looking for:

$names = array('Joe A. Smith','Joseph Able Smith','Joe Smith');
foreach ($names as $name) {
    $lp = strrpos($name,' '); // get the position of the last space in the string
    echo trim(substr($name,$lp)) . ', ' . trim(substr($name,0,$lp)) . "<br>";
}
?>

 

Ken

Try something like this:

<?php
function parseName($string) {
  if (preg_match('|(.+) ([a-z\-\']+)\z|i', $string, $match)) {
    $lname = $match[2];
    $fname = $match[1];
    $string = "$lname, $fname";
  }
  return $string;
}

$names = array('John Smith', 'John A. Smith', 'J. Smith');
$names = array_map('parseName', $names);
print_r($names);
?>

You can use this function

-----------------------------

<?php

function rev_name($str, $sep=' ')

{

$r = explode($sep, $str);

$txt = '';

for($i = count($r)-1;$i>=0;$i--)

{

$txt .= $r[$i];

if($i != 0)

{

$txt .= $sep;

}

}

unset($r);

return $txt;

}

?>

Use it in this way

echo rev_name('start mid last'); 

It would output

last mid start

[sorry cant use [ code ] Here]

Is this what you have wanted ?? or something else.

This is another function perhaps this is what you need

----------------------------------------------------

<?php

function rev_name($str, $sep=' ')

{

$r = explode($sep, $str);

if(count($r) > 2)

{

$txt = '';

for($i = count($r)-2;$i>=1;$i--)

{

$txt .= $r[$i];

if($i != 0)

{

$txt .= $sep;

}

}

return $r[count($r)-1].$sep.$r[0].$sep.$txt;

}

else

{

return $r[count($r)-1].$sep.$r[0];

}

}

echo rev_name('start mid last');

?>

Output

-----------------------------

last start mid

If you use rev_name('start last');

it will output is

last start

 

=====================================================

|| But none of those functions are Tested at all. ||

=====================================================

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.