-1

我有一个文本文件为“ a.txt”,

它有一个像图像一样的数据。我想逐行读取此文本文件并将*符号之间的每个条目保存并保存到单独的array.

注意:我想将每个信息保存到一个单独的数组中。

我想使用 PHP 代码来做到这一点。

先感谢您。

4

3 回答 3

0

您可以为此尝试爆炸。如果有效

 $handle = fopen("inputfile.txt", "r");
    if ($handle) {
        $array=array();
        while (($line = fgets($handle)) !== false) {
            // process the line read.
         $array[]=explode('*',$line);
        }

        fclose($handle);
    } else {
        // error opening the file.
    }
print_r($array);

输出

Array
(
    [0] => Array
        (
            [0] => 
            [1] => [(2R)-2-hydroxy-3-[2-(prop-2-en-1-yl)phenoxy]propyl](isopropyl)azanium   
            [2] => 250.1‌​81C15H24NO2
            [3] => 2
            [4] => 1
            [5] => 46
            [6] => 1
            [7] => 11
            [8] => 1.1266
            [9] => 1
            [10] => 18
            [11] => 6
            [12] => 18
            [13] => 6
            [14] => 4
            [15] => C[C@H](C)[NH2+]C[C@H‌​](COc1ccccc1CC=C)O"/> Vendors 

        )

)
于 2015-07-06T07:00:20.113 回答
0

我的代码

<?php
$ch = curl_init();
$fp = fopen("curl.txt", "w");
curl_setopt($ch, CURLOPT_URL, "htmlpage");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);

$lines = file("curl.txt");

/* Priint data of Supernatural Database in between the html tags */
foreach ($lines as $line){
 if (preg_match("!<td>(.*?)</td>!si", $line)){

/* string match and replace in html tags */

$string = str_replace(array("\r", "\n"), '', $line);
$patterns = array();
$patterns[0] = '/Name/';
$patterns[1] = '/Molecular weight/';

$replacements = array();
$replacements[0] = '*';
$replacements[1] = '*';


$txt = preg_replace($patterns, $replacements, $string);

$results = print_r($txt, true); 
file_put_contents('a.txt', print_r($results, true), FILE_APPEND);

/* remove all the html tags from output */
echo strip_tags($results, '<p><a><br>');
$strip_txt = strip_tags($results, '<p><a><br>');


 }
}
于 2015-07-06T09:29:55.987 回答
0
  • (2R)-2-羟基-3-[2-(丙-2-烯-1-基)苯氧基]丙基氮铵 * 250.181 *C15H24NO2 *2 *1 *46 *1 *11 *1.1266 *1 *18 *6 * 18 *6 *4 * CC@H[NH2+]CC@HO"/> 供应商* Ambinter 供应商代码:Ambmdy02300166 * (1S,17R,18R,19R)-17,18-dihydroxy-5,7-dioxa-12- azapentacyclo[10.6.1.0²,¹â°.0â´,â¸.0â¹âµ,¹â¹]nonadeca-2,4(8),9,15-tetraen-12-ium * 288.124 *C16H18NO4 *3 *2 *63 *1 *2 *0.9007 *5 *
于 2015-07-06T09:34:46.330 回答