$names = preg_split('/[\r\n]+/', $names); foreach($names AS $key => $val) { // do your queries here }