我有一个文本文件为“ 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[C@H](C)[NH2+]C[C@H‌​](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>');
}
}