所以我有一个文本块,我希望将其转换为PHP数组。最终目标是尽快将超过40年的数据输入MySQL数据库。
(1) FRANK SINATRA – Strangers In The Night (and charts)
(4) THE MERSEYS – Sorrow
(19) PAUL & BARRY RYAN – I Love Her
(NEW) EDDY ARNOLD – I Want To Go With You
理想情况下,如果数据可以解析为以下数组,那么我可以处理数据。任何想法如何做到这一点?我确实想过使用preg_split。还有其他人有什么想法吗?
$chartpos[] = "1"
$artist[] = "FRANK SINATRA"
$track[] = "Strangers In The Night"
$performancetype[] = "and charts"`
答案 0 :(得分:1)
这个正则表达式可以在$ match上找到一个带有' id'元素和名称'如果我正确理解您的格式,则包含所有相应位置的元素
$a = "(1) FRANK SINATRA – Strangers In The Night (and charts)
(4) THE MERSEYS – Sorrow
(19) PAUL & BARRY RYAN – I Love Her
(NEW) EDDY ARNOLD – I Want To Go With You";
preg_match_all('/\((?<id>[0-9]|NEW)\) (?<name>.*)/', $a, $matches);
print_r($matches);
答案 1 :(得分:0)
这会对你有帮助......
$re = '/\(([0-9NEW]+)\)\s+([A-Z& ]+) – (.*)/m';
$str = '(1) FRANK SINATRA – Strangers In The Night (and charts)
(4) THE MERSEYS – Sorrow
(19) PAUL & BARRY RYAN – I Love Her
(NEW) EDDY ARNOLD – I Want To Go With You';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);