-4
<?php
$string1 = "FARMER John";
$string2 = "OVERMARS DE Rafa";
$string3 = "VAN DER BELT Dick";
?>

To save the first and lastname in a mysql database I need to split the string. Above you can see a few examples.

What is the best way to split this string, note that the lastname is in caps.

Result should be:

ResultString1: John FARMER

ResultString2: Rafa OVERMARS DE

ResultString3: Dick VAN DER BELT

soccerfan
  • 11
  • 4
  • 1
    the best way would've been not packing first and last name in the same string in the first place. the second best way would've been reading [how to ask](http://stackoverflow.com/help/how-to-ask) and showing what you have tried so far. – Franz Gleichmann Oct 24 '16 at 12:05
  • I simply recommend you to have 2 Input like @FranzGleichmann already said – julianstark999 Oct 24 '16 at 13:30
  • Sometimes you will have to deal with the existing database tables en columns. It's obvious when you start a db with values you divide them upfront. – soccerfan Oct 24 '16 at 16:36

4 Answers4

1

This is a really bad idea.

There are tons of things that can go wrong if you're purely relying on letter case to signify something. And saving it all in another string just perpetuates the problem.

That said, you can mitigate (a small amount of) the badness by storing the new values separately, so as not to pass along unstructured information to the next unfortunate person who has to maintain your database. This code does that, while also allowing for multi-word first names (like "John Paul" or such).

I also made it handle these in array form, since I assume that you probably want to handle more than three names.

<?php

$string1 = "FARMER John";
$string2 = "OVERMARS DE Rafa Dafa";
$string3 = "VAN DER BELT Dick";
$strings = array(array('orig'=>$string1), array('orig'=>$string2), array('orig'=>$string3));
foreach($strings as $key=>$val){
    $oldwords = explode(' ',$val['orig']);
        foreach($oldwords as $word){
            if(preg_match('/[a-z]/',$word)){
              $firstname .= " ".$word;
            }else{
              $lastname .= " ".$word;
        }
    }
    $firstname = trim($firstname);
    $lastname = trim($lastname);
    $strings[$key]['newfirst'] = $firstname;
    $strings[$key]['newlast'] = $lastname;
    $firstname = "";
    $lastname = "";
}
print_r($strings);

?>
0

Follow the following steps:

  1. Convert string to array when space appears.
  2. Pop out the last array element(apparently the first name) using array_pop.
  3. Insert the popped out name in front using array_unshift
  4. Convert array back to string.

    $string3 = "VAN DER BELT Dick";
    
    $arr = explode(" ", $string3);
    array_unshift($arr, array_pop($arr));
    echo implode(" ", $arr);     // Dick VAN DER BELT
    
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0
$arr = explode(" ", $string3);
$last_word = array_pop($arr);
$final_string = ucfirst($last_word)." ".strtoupper(implode(" ", $arr));
echo $final_string;
himeshc_IB
  • 853
  • 4
  • 10
  • What if the person has 2 names? Like "GATES Billy William" – Mike Oct 24 '16 at 13:50
  • Then as per mentioned scenario, it will convert it like "William GATES BILLY". – himeshc_IB Oct 24 '16 at 13:55
  • The question states that what determines whether it is the first/last name is the case. – Mike Oct 24 '16 at 13:57
  • Provided code currently does't consider the case, it will just take your input string for example "GATES Billy William" and convert your string into required scenario which is William GATES BILLY, now it your case if person has 2 names then it is called human error.. – himeshc_IB Oct 24 '16 at 14:03
  • Having 2 names is quite common, actually. I wouldn't call it an error. – Mike Oct 24 '16 at 14:23
  • I had provided solution with the focus of scenario and requirement mentioned in the question, to handle different scenarios like you mentioned one need to update code according to the requirement... – himeshc_IB Oct 24 '16 at 14:49
0

As others said, your problem description is rather vague. However with a fixed pattern like this, you could very well split the names via a regular expression (given the surname is always in uppercase):

<?php
$strings = array("FARMER John", "OVERMARS DE Rafa", "VAN DER BELT Dick");

$regex = '~^([A-Z ]+\b)(\w+)$~';

foreach ($strings as $string) {
    if(preg_match($regex, $string, $match)) {
        $surname = trim($match[1]);
        $forename = trim($match[2]);
        echo "Surname: $surname, Forename: $forename\n";
    }
}

?>

See a working demo on ideone.com (plus the one on regex101.com).

Jan
  • 42,290
  • 8
  • 54
  • 79