Inverted Authors

6 views
Skip to first unread message

kristin robinson

unread,
Mar 5, 2011, 1:20:02 AM3/5/11
to Introduction to PHP, Jan-Apr 2011, Matt's Section
Hi folks, I confess I'm out of ideas.

Our array has the following as authors:

H. G. Wells
Frank Herbert
Ursula K. Le Guin

In the real world in order to invert them I'd simply call the first
and last names from the database and make them seperate items in the
array. The only way I see to invert things for this exercise is to
pick a character to break the string at. There's not always a
period. So I search for spaces. If I look for the first space from
the front H. G. Wells breaks wrong. First space from the back and
Ursula K. Le Guin breaks wrong (her last name is Le Guin). And what
if the array contained someone like Orson Scott Card (should be Card,
Orson Scott) or J. R. R. Tolkein? There isn't a logic structure I can
think of to figure out where the break is between first and last name
that works every time.

Any ideas welcome. You can see what I have at http://codepad.org/WTpnB2WX.
It shows I can invert things, but the logic for name distinction is
lacking.

I don't see it as possible. But I've been wrong before.

Ciao and thanks in advance.
Kristin

brotherhutch

unread,
Mar 5, 2011, 5:28:16 PM3/5/11
to Introduction to PHP, Jan-Apr 2011, Matt's Section
You achieved the same result as I, but in a different way. I also do
not see any non-complex way of reversing the names properly in all
cases. I think an extensive set of rules would have to be developed to
handle all of the different name formats out there. I suspect this is
beyond the scope of the question you have probably already satisfied
the requirements with what you've done.

Here is mine, just for an example of another method:

function lastNameFirst($name)
{
$lastName = trim(strrchr($name, " "));
$firstName = trim(rtrim($name, $lastName));
$reversedName = $lastName . ", " . $firstName;
return $reversedName;
}

-mark

On Mar 4, 10:20 pm, kristin robinson <kaykay...@gmail.com> wrote:
> Hi folks, I confess I'm out of ideas.
>
> Our array has the following as authors:
>
> H. G. Wells
> Frank Herbert
> Ursula K. Le Guin
>
> In the real world in order to invert them I'd simply call the first
> and last names from the database and make them seperate items in the
> array.  The only way I see to invert things for this exercise is to
> pick a character to break the string at.  There's not always a
> period.  So I search for spaces.  If I look for the first space from
> the front H. G. Wells breaks wrong.  First space from the back and
> Ursula K. Le Guin breaks wrong (her last name is Le Guin).  And what
> if the array contained someone like Orson Scott Card (should be Card,
> Orson Scott) or J. R. R. Tolkein?  There isn't a logic structure I can
> think of to figure out where the break is between first and last name
> that works every time.
>
> Any ideas welcome.  You can see what I have athttp://codepad.org/WTpnB2WX.
Reply all
Reply to author
Forward
0 new messages