Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: String validation
03-10-2013, 09:24 PM #1
- Join Date
- May 2009
- Thanked 1 Time in 1 Post
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
$strName = $_POST['customer'];
John Wayne Smith
John & Smith
The system I need to pass it too requires it to be
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.
03-10-2013, 10:26 PM #2
- Join Date
- Sep 2005
- Sydney, Australia
- Thanked 649 Times in 639 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.
Peter Allan Smith
here Smith is the surname and the spot to break it is the second space.
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
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.