修改文本文件中的行并保存到新的PHP数组中

时间:2016-06-01 02:54:28

标签: php text

我有以下文本文件(Query1.cif):

ATOM 1  P P     . A A 1 1 1 ? 25.393 -14.093 8.181  1.00 55.25 ? ? ? ? ? ? ? 23 A X P     23 A X P     2

ATOM 2  O OP1   . A A 1 1 1 ? 25.462 -13.992 9.643  1.00 56.56 ? ? ? ? ? ? ? 23 A X OP1   23 A X OP1   2 

ATOM 3  O OP2   . A A 1 1 1 ? 25.063 -12.918 7.334  1.00 53.05 ? ? ? ? ? ? ? 23 A X OP2   23 A X OP2   1 

我想做的是:

1)阅读文本文件中的每一行

2)检查最后一栏。如果该值不是1,请将其更改为1

3)将所有行保存到新数组中。

我遇到了第三步的问题。我不确定将每一行保存到新数组的正确方法。我还需要访问foreach循环之外的数组值。

以下是pastebin中代码的链接 - http://pastebin.com/PLY9FaLN

//Query 1 would be an array containing the atomic coordinates of motifs/nucleotides of interest

$Query1 = explode("\n", file_get_contents('Query1.cif'));

foreach ($Query1 as $line) {

    $line = trim($line);

    $line = (explode(" ",$line));

    $last_index = count($line) - 1;

    echo "</br>"; echo "</br>";

        if (strcmp($line[$last_index], '1') !== 0) {
            //echo '$var1 is not equal to $var2 in a case sensitive string comparison'."</br>";
            $line = str_replace ($line[$last_index], '1', $line);

        }

// I need to save each line into an array and access them outside the loop

}

// I want to access the array values here

2 个答案:

答案 0 :(得分:0)

我的工作做得很好。请注意我使用字符串格式,因为我不想创建一个文件来执行此操作。您仍应使用file_get_contents

代码

<?php
$query_string = <<<EOD
ATOM 1  P P     . A A 1 1 1 ? 25.393 -14.093 8.181  1.00 55.25 ? ? ? ? ? ? ? 23 A X P     23 A X P     2

ATOM 2  O OP1   . A A 1 1 1 ? 25.462 -13.992 9.643  1.00 56.56 ? ? ? ? ? ? ? 23 A X OP1   23 A X OP1   2 

ATOM 3  O OP2   . A A 1 1 1 ? 25.063 -12.918 7.334  1.00 53.05 ? ? ? ? ? ? ? 23 A X OP2   23 A X OP2   1
EOD;

$Query1 = explode("\n", $query_string);
$new_arr = [];
//Creates a new array;
foreach ($Query1 as $line) {

    $line = trim($line);
        //trims the line of any extra spaces
        if(strlen($line)==0) continue;
        //If this is one of the lines with no characters on it (like the ones between ATOM 1 and ATOM 2, or ATOM 2 and ATOM 3), skip this particular loop
      //aka "continue" the loop.
    $last_char = substr($line,-1);
        //Gets the last character with the substr() function. providing -1 starts at the first character at the end of the string.
        if($last_char !== '1'){
            //If the last line is a 1, we go into this if
            $line = substr_replace($line,'1',-1);
            //Replace the substring content of $line, starting at the first character at the end, with '1'. 
            //This overwrites the last character.
        }
    $new_arr[] = explode(' ',$line);
    //$new_arr[] adds whatever is on the other side of the equal sign to the next index iteration in the array. 
    //The first call adds the content on the other line to $new_arr[0], the second loop will add it to $new_arr[1], etc.


// I need to save each line into an array and access them outside the loop

}
echo '<pre>';
    print_r($new_arr);
echo '</pre>';
// I want to access the array values here
?>

输出

这是浏览器中print_r调用的输出:

Array
(
    [0] => Array
        (
            [0] => ATOM
            [1] => 1
            [2] => 
            [3] => P
            [4] => P
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => .
            [10] => A
            [11] => A
            [12] => 1
            [13] => 1
            [14] => 1
            [15] => ?
            [16] => 25.393
            [17] => -14.093
            [18] => 8.181
            [19] => 
            [20] => 1.00
            [21] => 55.25
            [22] => ?
            [23] => ?
            [24] => ?
            [25] => ?
            [26] => ?
            [27] => ?
            [28] => ?
            [29] => 23
            [30] => A
            [31] => X
            [32] => P
            [33] => 
            [34] => 
            [35] => 
            [36] => 
            [37] => 23
            [38] => A
            [39] => X
            [40] => P
            [41] => 
            [42] => 
            [43] => 
            [44] => 
            [45] => 1
        )

    [1] => Array
        (
            [0] => ATOM
            [1] => 2
            [2] => 
            [3] => O
            [4] => OP1
            [5] => 
            [6] => 
            [7] => .
            [8] => A
            [9] => A
            [10] => 1
            [11] => 1
            [12] => 1
            [13] => ?
            [14] => 25.462
            [15] => -13.992
            [16] => 9.643
            [17] => 
            [18] => 1.00
            [19] => 56.56
            [20] => ?
            [21] => ?
            [22] => ?
            [23] => ?
            [24] => ?
            [25] => ?
            [26] => ?
            [27] => 23
            [28] => A
            [29] => X
            [30] => OP1
            [31] => 
            [32] => 
            [33] => 23
            [34] => A
            [35] => X
            [36] => OP1
            [37] => 
            [38] => 
            [39] => 1
        )

    [2] => Array
        (
            [0] => ATOM
            [1] => 3
            [2] => 
            [3] => O
            [4] => OP2
            [5] => 
            [6] => 
            [7] => .
            [8] => A
            [9] => A
            [10] => 1
            [11] => 1
            [12] => 1
            [13] => ?
            [14] => 25.063
            [15] => -12.918
            [16] => 7.334
            [17] => 
            [18] => 1.00
            [19] => 53.05
            [20] => ?
            [21] => ?
            [22] => ?
            [23] => ?
            [24] => ?
            [25] => ?
            [26] => ?
            [27] => 23
            [28] => A
            [29] => X
            [30] => OP2
            [31] => 
            [32] => 
            [33] => 23
            [34] => A
            [35] => X
            [36] => OP2
            [37] => 
            [38] => 
            [39] => 1
        )

)

答案 1 :(得分:0)

$Query1 = explode("\n", file_get_contents('Query1.cif'));
$newarray =""; //create new array

foreach ($Query1 as $line) {

    $line = trim($line);

    If  (substr($line, strlen($line)-1, strlen($line)) != 1)  $line = substr($line, 0, strlen($line)-1)."1";

    $newarray[] = $line;

}

Var_dump($newarray);