Reputation: 303
I have a text document with a slew of email addresses which I converted from a pdf. here is an example of of what it looks like:
name1;[email protected];;;
name2;[email protected];;;
name3;[email protected];;;
name4;[email protected];;;
name5;[email protected];;;
etc... 600+ contacts
anyone know to to write a simple php pattern/expression/regex I can use to separate the name and email one by one so I can put in database?
the database of course would be a simple: id | contact | email
any help would be gladly appreciated!
I forgot to mention, I would like to do it in php. I will incorporate the code into a form for future usage.
Upvotes: 0
Views: 70
Reputation: 5256
In PHP, you can split a string using the explode function..
$parts = explode(';', $inputString);
The returned array contains each part separated by ;
.
For this, each line in your text document has to be given as inputString
. So loop through the array returned by
preg_split('/\\n/',$docContent)
and call explode
with each element. The above preg_split
returns an array with each line of the input as an element.
Combining both,
$lines = preg_split('/\\n/',$docContent);
foreach($lines as $line) {
$parts = explode(';', $line);
//$parts[0] is name and $parts[1] is email. ignore remaining elements
}
Note : I have only a little knowledge in php. There may be better code.
Upvotes: 3
Reputation: 55619
How about something like:
LOAD DATA INFILE 'yourFile'
INTO TABLE yourTable
FIELDS TERMINATED BY ';'
LINES TERMINATED BY ';;;\n'
Upvotes: 1
Reputation: 31314
assuming that by "contact" you mean the very first field of each line (which says 'contact' for all shown values), something like this will work:
cat contacts.txt | awk {'split($2,A,";"); print A[1]"|"$1"|"A[2]}'
Upvotes: 0