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!
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.
Input was "Able, Cynthia A." "Adams, Rowena D." Output by echo "$last<br>"; produced Cynthia Rowena Where do I call for their first names?
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 Cheers!