我有一个文本文件为“a.txt
”,提取信息,并且每个数据保存到一个单独的阵列
其具有类似于图像数据。我想逐行阅读这个文本文件,并将每个条目保存在*
符号之间并保存到单独的array
中。
注意:我希望将每个信息保存到一个单独的数组中。
我想用PHP代码来做到这一点。
预先感谢您。
我有一个文本文件为“a.txt
”,提取信息,并且每个数据保存到一个单独的阵列
其具有类似于图像数据。我想逐行阅读这个文本文件,并将每个条目保存在*
符号之间并保存到单独的array
中。
注意:我希望将每个信息保存到一个单独的数组中。
我想用PHP代码来做到这一点。
预先感谢您。
你可以尝试为此爆炸。如果它工作
$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
)
)
我的代码
<?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>');
}
}
显示您的代码,任何尝试? – MKD
我不知道如何实现它。请建议...这是我的输出文本文件格式。 – ombioinfo
\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