-1

I have a text file as "a.txt",

which has a data like the image. I want to read this text file line by line and save each entry between the * symbol and save into a separate array.

Note: each information I want to save into a separate array.

I want to do this using PHP code.

Thank you in advance.

Sagar Naliyapara
  • 3,971
  • 5
  • 41
  • 61
ombioinfo
  • 33
  • 4

3 Answers3

0

you can try explode for this . if it works

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

output

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 

        )

)
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
  • Hi Ris, Thank you for your reply and code. Printing is not a problem. using explode, I am able to print separately. But, I want to store each data into a separate array so that I can insert into my database in a automated way. – ombioinfo Jul 06 '15 at 07:21
  • you can add `$array` into db with for loop or foreach loop . what's the problem??/ – Manoj Dhiman Jul 06 '15 at 08:22
  • Actually, I am scraping some database and trying to pull large data using file get contents from html page and removing unwanted fields from there and able to print in webpage as well as a text file successfully but my issue is to read the text file line by line and find the matches and insert into database. Here is my code for whole-- – ombioinfo Jul 06 '15 at 09:16
  • $ch = curl_init(); $fp = fopen("curl.txt", "w"); curl_setopt($ch, CURLOPT_URL, "somehtml page"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FILE, $fp); $lines = file("curl.txt"); foreach ($lines as $line){ if (preg_match("!(.*?)!si", $line)){ $string = str_replace(array("\r"), '', $line); $patterns = array(); $patterns[0] = '/Name/'; $replacements = array(); $replacements[0] = '*'; $txt = preg_replace($patterns, $replacements, $string); $results = print_r($txt, true); file_put_contents('a.txt', print_r($results, true), FILE_APPEND); }} – ombioinfo Jul 06 '15 at 09:24
0

My code

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


 }
}
ombioinfo
  • 33
  • 4
0
  • (2R)-2-hydroxy-3-[2-(prop-2-en-1-yl)phenoxy]propylazanium * 250.181 *C15H24NO2 *2 *1 *46 *1 *11 *1.1266 *1 *18 *6 *18 *6 *4 * CC@H[NH2+]CC@HO"/> Vendors* Ambinter Supplier code: 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 *
ombioinfo
  • 33
  • 4