dtz63853 2017-03-22 23:25 采纳率: 0%
浏览 46
已采纳

将格式化文本块转换为PHP数组

So I have a block of text which I wish to convert into a PHP array. The end goal is to enter over 40 years worth of data into MySQL database as quickly as possible.

(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

So ideally, if the data could be parsed into the following arrays so I can process the data. Any ideas how to do this? I did think of using preg_split. Does anyone else have any ideas?

$chartpos[] = "1"
$artist[] = "FRANK SINATRA"
$track[] = "Strangers In The Night"
$performancetype[] = "and charts"`
  • 写回答

2条回答 默认 最新

  • douduan5753 2017-03-23 00:10
    关注

    This regular expression would get you an array at $matches with an 'id' element and a 'name' element containing all the corresponding position if i understood your format correctly

    $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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化