Jump to content

preg_split


lindm

Recommended Posts

I have the following code extract data from a text file:

$contents = file("textfile.txt"); //chose file

foreach($contents as $line_value)
{
list($cat, $int, $id, $num) = preg_split("/(\t| )/",$line_value);

    $cat = str_replace('#', '', $cat);

    $number[$cat][$int][$id]= trim($num);
}

 

The text file contents normally looks like this:

#RES -1 6922 144897.52
#RES -1 6970 16894.59
#RES -1 6981 1110.00
#RES -1 6993 500.00

 

echo $number["RES"][-1][6981]; would return 1110.00

 

All fine. Now I have a text file where the whitespace varies like this:

#RES -1 6922 144897.52
#RES -1 6970       16894.59
#RES -1 6981       1110.00
#RES -1 6993 500.00

 

echo $number["RES"][-1][6981]; would then return 0 which I of course don't want. Any easy way to solve this?

 

 

Link to comment
https://forums.phpfreaks.com/topic/184925-preg_split/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.