1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help with a tricky string parsing/replacement

Discussion in 'PHP' started by jsilvagi, Jul 28, 2009.

  1. #1
    Ok, I have been pulling my hair out on this one and I am hoping someone will be able to point me in the right direction.

    I am working with a web database for my company and I rather than enter in all of our 500 employees I was hoping to take our company list from a CSV and push it into my mySQL DB.

    Here is what I have to work with :

    "Ringger, Emily H."
    "Roberts Kendall, Katelyn E."
    "Robertson, John P."
    "Robinson, Christopher S."
    "Robinson, Tamara L"
    "Rogers, Benjamin"
    "Roque, Catherine R."
    "Roseman, Monica M."
    "Ross, Chelsey S."

    What I need to do is break from down into first_name and last_name so I can import them into my DB. Problem I am facing is middle inital that sometimes have periods and sometimes do not. I have been able to clean it up, but I cannot get it into a format that is usable for me just yet.

    So basically in the end I need to break each persons name into a variable $first_name and $last_name that can then go into my sql query.

    ANY help is greatly appreciated.

    Thanks!
     
    jsilvagi, Jul 28, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try doing this.

    Use PHP's file() function to get the list into an array.
    http://us.php.net/manual/en/function.file.php

    You'll have to do 2 explodes, to get the data you need. First explode by a comma, which will give you last name, and then firstname and middle initial. Then explode by a space, and you will separate the first and middle. It may take a little logic to get a clean break, but shouldn't be too difficult.

    
    $lines = file('/path/to/file');
    
    foreach($lines as $num => $line)
    {
    
    $break= explode(",", $line);
    
    $last = $break[0];
    
    $break2 = explode(" ", trim($break[1]));
    
    $last = $break2[0];
    
    if($break2[1])
    {
    
    $middle = str_replace('.','',$break2[1]);
    
    } else
    {
    $middle = '';
    }
    
    //Format or insert or whatever else here.
    
    }
    
    
    PHP:
    You can then insert these directly, or format them however you need. This is untested but should work with or without minor modification. If the " are actually part of the string, you probably want to strip those off as well.
     
    jestep, Jul 28, 2009 IP
    jsilvagi likes this.
  3. jsilvagi

    jsilvagi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Input was

    "Able, Cynthia A."
    "Adams, Rowena D."

    Output by echo "$last<br>"; produced

    Cynthia
    Rowena

    Where do I call for their first names?
     
    jsilvagi, Jul 28, 2009 IP
  4. Leron

    Leron Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    53
    #4
    Try this:
    
    $string= '
    "Ringger, Emily H."
    "Roberts Kendall, Katelyn E."
    "Robertson, John P."
    "Robinson, Christopher S."
    "Robinson, Tamara L"
    "Rogers, Benjamin"
    "Roque, Catherine R."
    "Roseman, Monica M."
    "Ross, Chelsey S."
    NAMES';
    
    preg_match_all('/\"(.*),+[\s]+(.*?)[a-zA-Z]?[\.]?\"/',$string,$array);
    
    foreach($array[1] as $key=>$value){
    	echo $value." ".$array[2][$key]."<Br>";
    }
    
    PHP:
    It does not have to be in a string. Can be placed in a csv files as stated.

    Hope this helps :D
    Cheers!
     
    Leron, Jul 28, 2009 IP
    jsilvagi likes this.
  5. jsilvagi

    jsilvagi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually I just played around with it and got it!!! Thanks a ton man, you are a life saver!
     
    jsilvagi, Jul 28, 2009 IP