Jump to content

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. ||

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.