I am trying to separating data from Isam files looping it line by line using fgets and with substr to get correct data from correct position of line.
Source data:
000000124325424Productname Type Price Weight Unit Unitcode
000000124325324Productname2 Type2 Price2 Weight2 Unit2 Unitcode2
Problem is that substr does not recocnize those extra spaces between data and therefore position of data is changing every line.
Substr is "seeing" line like this:
000000124325424Productname Type Price Weight Unit Unitcode
Is there any way to preserve the spaces between items?
After editing the question it hits me that you perhaps are dealing with a tab delimited string \t
. Either way, sanitize each string before whatever you are trying to do with substr()
:
$s = preg_replace('/\s+/', ' ', $s);
This will strip out multiple blanks " "
=>" "
, tabs \t
and even line breaks.
Perhaps you can find explode()
as a better approach after sanitizing :
explode(" ", str_replace("\t", " ", $line1))[0]
will always be first item, i.e product code without extra spaces.