dpus81500574 2011-12-01 22:59
浏览 220
已采纳

带有不规则空格和制表符的文件按列分割/爆炸

So I have a very old file with thousands of lines (I guess generated by hand) and I'm trying to move them into a rdb, but the lines don't have a format/pattern to convert into columns. Say for example the lines in the file looks like:

blah   blahsdfas    laslkdlasdj      aksdjla
sldks  slslsl      lsdlksldj           lsdjlfslk

I could say it has four fields when I look at it, primarily tried using awk but it wasn't printing the column as expected because the space between a column is not tab or with an equal space count.

You guys think its possible to extract? If yes can someone help with a php snippet?

  • 写回答

2条回答 默认 最新

  • doufeng2877 2011-12-01 23:03
    关注

    Using preg_split(), you can break the line up using one or more whitespace characters as the delimiters:

    $lines = file('filename', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach($lines as $line)
    {
        $pieces = preg_split('/\s+/', $line);
        // do something with pieces
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建