Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts

    reverse strings.

    Hi I was wondering if someone could help me figure out how to use name strings obtained using substr() to create a new reverse name string? for example
    firstname surname reversed to surname, firstname?? adding a comma and space in between??

    Many thanks
    Last edited by helen11; 11-20-2011 at 04:42 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    Can you provide the code used to obtain the name via SubStr()?

    Thanks,
    ~Jordan
    Remember The Thank You Button Is Your Friend =]

    [ PHP / Javascript / VisualBasic / HTML / DHTML / CSS / .NET / MySQL ]

  • #3
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts
    $fullname = "Helen Conor";
    $len = strlen($fullname);
    print ("Length=$len");
    print ("$fullname");

    $firstname = substr($fullname, 0, 6);
    $secondname = substr($fullname, 7, 15);
    print "firstname is $firstname and secondname is $secondname";

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $name 'firstname surname';

    $name preg_split'/\s+/'$name2PREG_SPLIT_NO_EMPTY );
    $name array_reverse$name );
    $name implode', '$name );

    echo 
    $name// surname, firstname 
    The big gotcha with this solution is that it will take anything before the first space as the first name, and everything else is the last name.

    So with the name Billy Joe Smith, the first name will be Billy, and the surname will be Joe Smith... when we would expect the first name to be Billy Joe.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 Posts
    This will over come the two first name problem and works with one first name:

    Code:
    $name = 'Billy Joe Smith';
    echo strrchr($name,' ').', '.trim(str_replace(strrchr($name,' '), '', $name));
    For clarity the last name is can be found with
    $last = strrchr($name,' ');

    before the substitution.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Keep in mind the last name can possibly be more than one word:
    PHP Code:
    $name 'Billy Joe Smith, Jr.';

    $last strrchr$name' ' );

    echo 
    $last// Jr. 
    ...which is what I was alluding to in my first reply. You may have it work on 95% of two-word names out there, but there will always be the possibility of a two-word first name, last name, or both.
    Last edited by kbluhm; 11-21-2011 at 02:56 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •