Jump to content

[SOLVED] Parasing data


cpd

Recommended Posts

Hello, im currently trying to parase some data from a file. Ive done the "file("htttp://www.example.com")" function and split all the rows into arrays however i need to now explode the spaces between the columns, example below.

 

Column1 Column2 Column3 Column4 Column5  <= take this row

Column1 Column2 Column3 Column4 Column5

Column1 Column2 Column3 Column4 Column5

 

$File = file(link);

 

$File[0]; <= This is the first row

 

$Data = explode(" ", $File[0]);

 

print $Data[3]; <=I presumed this would print column four but it doesnt it just does nothing. $Data[0] prints Column1 and $Data[1] will print the last four columns.

 

At this point im not sure if im making much sense but my target is to split the columsn into arrays and then be able to use the data from there. Could someone please tell me how to do this.

 

Regards Chris

Link to comment
Share on other sites

perhaps the spaces between the columns aren't really spaces, but something else, like a tab? When you explode ' ' it will explode only a literal space, so if it is actually a tab char (may look like a space char) it will not recognize and explode it.  You can try doing this instead of explode:

 

$Data = preg_split('~\s~',$File[0]);

 

preg_split works much the same way as explode, but throws ability to use regular expressions into the mix.  \s is a shorthand character class for all "space" type characters.

Link to comment
Share on other sites

Right, after some research ive found some stuff out which i didnt know however my problem still stands.

 

Ive found out that when you have a text file that has double, tripple or however many spaces between columns, the "file" function splits each line into an array and replaces all double spaces tripple spaces etc, with single spaces.

 

So from that i now know that all i need to do is "explode" or "preg_split" each array to get the single columns however when i do this (baring in mind i havnt tried preg_split yet, still need to research how it works) it only creates two arrays eg:

 

$file = file(something);

 

$line = explode(" ", $file[0]);

 

print $line[0]; // This gives the first column value

print $line[1];; // This gives the other columns however many there are

 

Im gonna try the preg_split. Is there any chance someone could explain why it only explodes the first space and then does nothing to the other spaces?

 

Thank yooouuu =]

Link to comment
Share on other sites

Ive got another little problem, im not sure how to split difference characters with the preg_split functions. Is there a link that will show me what to use for splitting different characters.

 

~\s~ will split a space

what will split a forward slash?

Link to comment
Share on other sites

Ive found out that when you have a text file that has double, tripple or however many spaces between columns, the "file" function splits each line into an array and replaces all double spaces tripple spaces etc, with single spaces.

 

No, the file function takes a file and uses the carriage return (\n) as a delimiter to make an array out of each "line" in the file.  I think you meant to say that that's what explode does, which is what I said in my previous post.

 

Im gonna try the preg_split. Is there any chance someone could explain why it only explodes the first space and then does nothing to the other spaces?

 

I explained that in my previous post.  A space char and a tab char look the same to us visually, but they are not the same thing.  Explode is a simple function that will take one thing and explode the string using that one thing as the delimiter. Since spaces and tabs are different, and you specified space, it did exactly what you told it to do: explode at the space.  Since it only found the one space, so you ended up with 2 elements of the array.

 

Ive got another little problem, im not sure how to split difference characters with the preg_split functions. Is there a link that will show me what to use for splitting different characters.

 

~\s~ will split a space

what will split a forward slash?

 

A forward slash will match a forward slash.  preg_split is a regular expression (regex) function.  That is, it works by using regular expressions.  The very short explanation of regex is pattern matching using various degrees of rules and "wild cards."  For instance, if you've ever played poker and jokers are wild, that's regex in principle. 

 

Or for example, if I have the string "bob bib bab" and I want to match for "bob" I would literally specify "bob" in the pattern.  But if I wanted to for instance match any word that starts with 'b' and ends with 'b' I could make my pattern "b.b" because the dot is a "wild card" which stands for any 1 character (bit more complex than that but that's the gist of it). 

 

The ~ ~ on either end is the pattern delimiter.  It tells preg_split (or any other regex function) what the start and end of the pattern is.  You can use a lot of different things as delimiters, as long as you escape them if you need to use that particular character in the pattern.

 

As far as resources for this stuff goes: There is a sub-forum of this php help forum dedicated to regex (which btw this thread will be moved to it).  There are stickies in that forum for resources.

Link to comment
Share on other sites

No, the file function takes a file and uses the carriage return (\n) as a delimiter to make an array out of each "line" in the file.

 

I think you meant newline (\n), as a carriage return is \r.

 

 

The ~ ~ on either end is the pattern delimiter.  It tells preg_split (or any other regex function) what the start and end of the pattern is.  You can use a lot of different things as delimiters, as long as you escape them if you need to use that particular character in the pattern.

 

@OP.. To expand on what CV is saying, a delimiter can be any non-whitespace, non-alphanumeric character (other than a backslash). Delimiters can also include opening and closing punctuation sets like <...>, {....}, (.....) or [...] (although I personally wouldn't recommend using those). Commonly you'll see / as the delimiter of choice.. but since file paths are commonly used in patterns, this means (as CV mentioned) you'll have to escape any / inside the pattern that is delimited by the / character (this is not necessarily true for all delimiters.. there are oddball circumstances that might not require escaping.. but I digress). I would recommend going with less used characters like #...# or ~....~ for example.. this reduces the chances of needing to escape characters within the pattern.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.