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 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    160
    Thanks
    71
    Thanked 1 Time in 1 Post

    String validation

    Hi guys, got a slight problem. I grab data from my internal system, and I need to pass it to an external system which wants it in another format. So I get a customer name like so
    Code:
    $strName = $_POST['customer'];
    That name could be anything
    null
    John Smith
    John-Smith
    John Wayne Smith
    John
    John & Smith
    etc

    The system I need to pass it too requires it to be
    $strBillingFirstnames;
    $strBillingSurname;

    So a firstname and a surname. If my String contains only 1 word (no surname), surname will have to be set to 000. Anything which is empty should be 000 (so if my string is completely empty, both firstname and surname would be 000). But then how could I split my String bearing in mind that the deliminator could essentially be anything?

    In summary, I have one String which could be anything. How do I get this correctly into 2 Strings?

    Any advice on this problem appreciated.

    Cheers

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,611
    Thanks
    0
    Thanked 645 Times in 635 Posts
    With the way you are currently allowing names to be entered there is no way to tell what part of the name is the surname.

    For example:

    Peter Allan Smith

    here Smith is the surname and the spot to break it is the second space.

    Peter Smith

    now the surname is the part after the first space

    George van der Who

    again the surname is after the first space but itself now contains spaces

    Smith, George

    here someone has entered their surname first


    Given that the field can currently contain the name in any format the safest way to handle it would be to split the one field into two in your system so that you have a separate field for each part of the name to be passed to the other system. That way you only need to deal with splitting the names already in your database and any future ones will be correctly split by their owners. Then you can do an analysis of the names that you currently have stored in order to work out how best to split those specific names into the two fields. For example if you don't have any names that look like the last two examples I listed then splitting on the last space might be a good starting point and then consider what exceptions to make from there. If you have lots of examples of the last of my examples then first testing for a comma and taking the surname as whatever precedes it might be the first test.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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