I hope someone can help me. I need to extract certain details (“Full Name and Surname”, “Email”, “Contact Number) from a MySQL DB but can’t figure out how to do this. The application in question, “Grunion Contact Form” for WordPress saves the data in the DB, in an array, as follows:
Array
(
[Full Name and Surname:] => Sharon Somerset
[Email] => sharon@x.x.x
[Contact Number:] => 08xxxxxx
[Alternative Number] => 011xxxxxx
[Please list convenient time to be contacted] =>
[Medical Aid Quote] => Yes
[Insurance Quote] =>
[Policy Update] =>
[Friend's Name and Surname:] =>
[Friend's Contact Number:] =>
[How did you find us?] => Google
)
Each entry is in it’s own database row, inside “longtext” field.
How can I easily extract “Full Name and Surname”, “Email”, “Contact Number” data from a few thousands rows like this, and re-save in a simple table or CSV file to import into something like phplist
This should get you started – its simple regular expression matching on the text field. This will take a while to run through – but it does what you need.
<?php $string = "Array ( [Full Name and Surname:] => Sharon Somerset [Email] => sharon@x.x.x [Contact Number:] => 08xxxxxx [Alternative Number] => 011xxxxxx [Please list convenient time to be contacted] => [Medical Aid Quote] => Yes [Insurance Quote] => [Policy Update] => [Friend's Name and Surname:] => [Friend's Contact Number:] => [How did you find us?] => Google )"; $string = preg_replace("/Array\n\(\n/","",$string); //remove first line $string = preg_replace("/\)$/","",$string); //remove last line $lines = explode("\n",$string); //split on new lines print_r($lines); foreach($lines as $l) { preg_match("/\[(.+)\] => (.*)$/",$l,$matches); //$matches[1] will contain the label //$matches[2] will contain the value print_r($matches); } ?>