2015-07-06 111 views
-1

我有一个文本文件为“a.txt”,提取信息,并且每个数据保存到一个单独的阵列

其具有类似于图像数据。我想逐行阅读这个文本文件,并将每个条目保存在*符号之间并保存到单独的array中。

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

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

预先感谢您。

+1

显示您的代码,任何尝试? – MKD

+0

我不知道如何实现它。请建议...这是我的输出文本文件格式。 – ombioinfo

+0

\t * [(2R)-2-羟基-3- [2-(丙-2-烯-1-基)苯氧基]丙基](异丙基)azanium \t \t * 250.181 \t * C15H24NO2 \t * 2 \t * 1 \t * 46 \t * 1 \t * 11 \t * 1.1266 \t * 1 \t * 18 \t * 6 \t * 18 \t * 6 \t * 4 \t * \tç[C 3 H](C)[NH 2 +] C [C^@ H] (COc1ccccc1CC = C)O“/>供应商* – ombioinfo

回答

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[[email protected]](C)[NH2+]C[[email protected]‌​](COc1ccccc1CC=C)O"/> Vendors 

     ) 

) 
+0

Hi Ris,谢谢你的回复和代码。将每个数据存储到一个单独的数组中,以便我可以插入我的数据库以自动化的方式。 – ombioinfo

+0

您可以使用for循环或foreach循环将'$ array'添加到db中。有什么问题?/ – MKD

+0

其实,我正在抓取一些数据库,并尝试使用文件从HTML页面获取内容,并从那里删除不需要的字段,并能够在网页以及文本文件成功打印大数据,但我的问题是逐行读取文本文件并查找匹配并插入到数据库中。这里是我的整个代码 - – ombioinfo

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>'); 


} 
} 
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 *供应商编码:Ambmdy02300166 *(1S,17R,18R,19R)-17,18-二羟基-5,7-二氧杂-12-氮杂五环[10.6.1.0 (8),9,15-四烯-12- * * 288.124 * C 16 H 18 NO 4 * 3 * 2 * 63 * 1 * 2 * 0.9007 * 5 *
相关问题