parsing - PHP file parse--problems with last item in row

I have a text file on the server that is generated from a database export. I have no control over that file. I am trying to parse it in PHP to get some of the data to display on a web page. When I try to search for the last item in a row, it can't find it.
Here is a portion of the text file:
ERMHDR 8.2 2017-02-02 Project admin Primavera Admin dbxDatabaseNoName Project Management USD
%T CURRTYPE
%F curr_id decimal_digit_cnt curr_symbol decimal_symbol digit_group_symbol pos_curr_fmt_type neg_curr_fmt_type curr_type curr_short_name group_digit_cnt base_exch_rate
%R 1 2 $ . , #1.1 (#1.1) Dollar USD 3 1
%R 10 2 $ . , #1.1 (#1.1) Argentine Peso ARS 3 3.077
%R 11 2 A$ . , #1.1 (#1.1) Australian Dollar AUST 3 1.208
%R 13 2 R$ . , #1.1 (#1.1) Brazilian Real BRL 3 2.014
%R 14 2 ВЈ . , #1.1 (#1.1) British Pound U.K. 3 0.501762
%R 15 2 CA$ . , #1.1 (#1.1) Canadian Dollar CAD 3 1.10573
%R 16 2 Y . , #1.1 (#1.1) Chinese Yuan CNY 3 7.694
%R 17 2 € . , #1.1 (#1.1) EURO EUR 3 0.739088
%R 20 2 HK$ . , #1.1 (#1.1) Hong Kong Dollar HKD 3 7.81967
%R 21 2 Rs . , #1.1 (#1.1) Indian Rupee INR 3 40.67
%R 23 2 ВҐ . , #1.1 (#1.1) Japanese Yen JPY 3 120.167
%R 24 2 K . , #1.1 (#1.1) Korean Won KRW 3 924.743
%R 25 2 N$ . , #1.1 (#1.1) Mexican Peso MXN 3 10.7938
%R 26 2 R . , #1.1 (#1.1) Russian Rouble RUB 3 25.8085
%R 28 2 Sk . , #1.1 (#1.1) Swedish Krona SEK 3 6.80579
%R 29 2 kr . , #1.1 (#1.1) Swiss Franc CHF 3 1.21864
%R 30 2 NIS . , #1.1 (#1.1) Israel Shekel ILS 3 3.96384
This works perfectly. It finds the line that has "10" as the second value and returns the row number.
<?php
$txt_file = file_get_contents('sandbox/uploads/sample3.xer');
$rows = explode("\n", $txt_file);
array_shift($rows);
foreach($rows as $row => $data)
{
$row_data = explode("\t", $data);
if($row_data[1] == "10"){ //THIS IS THE LINE GIVING ME PROBLEMS
echo 'Row #: ';
echo $row;
echo ' ';
echo $row_data[1];
echo '<br>';
}
}
?>
However, if I change the indicated row to this, it does not work.
if($row_data[1] == "CURRTYPE"){
I think the issue is that when I exploded the file, I somehow put a special character at the end of each line and I need to include that character in my if statement, but I can not figure out what that special character is. I have tried adding a space, \n, \t.
Can anyone help???
Answer
Solution:
Usetrim
to remove any additional chars at start or end of string (e.g. whitespace).
if (strcasecmp(trim($row_data[1]), 'currtype') === 0)
Answer
Solution:
Try to read it with file. This will turn each line as an array. I tried it on your text portion and got all lines. and with explode(' ', THE LINE YOU WANT); will give you each word in that line
$test_file = file('sample3.xer');
$line_4 = $test_file[4];
$string_line = explode(' ', $line_4);
foreach ($string_line as $k => $v) {
if (!empty($v)) echo $v.'<br>';
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.