File with irregular spaces and tabs split/explode columnwise

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?

Answers


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
}

Need Your Help

Trying to display game information after selecting a team from the drop down

javascript xml xslt xslt-1.0

I have data values for teams and the games that are played in a XML file that has the following structure

E_NOINTERFACE error while creating com local server

winapi visual-c++ com

I have created a COM local server(exe) sample project using ATL wizard in VS2008.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.