Welcome Guest, Not a member yet? Register   Sign In
Extracting string from between two strings multiple times..
#1

[eluser]ProImage Web[/eluser]
I'm trying to read a file and extract a string between two other strings multiple times.. I can read the file, and extract the first string without incident, however, i'm having trouble with the loop..
The idea is to enter a start and stop string, then parse the file extracting the desired string from between the start and stop multiple times and write the results to another file. Any help would be appreciated,

here's the code i have so far.

Code:
$filename = $this->upload->file_name;
$file_path = './uploads/';
$write_path ='./uploads/data.php';
$orig_name = $this->upload->orig_name;
$start_string = $this->input->post('start_string');
$stop_string = $this->input->post('stop_string');
            
$data = read_file($file_path.$filename);
$deduct = strlen($start_string); // needed for substr to remove the start_string from the result
            
$between = substr(substr($data, strpos($data, $start_string), strpos($data, $stop_string) - strpos($data, $start_string)), $deduct);
echo $between;

What i'd like to happen is this:

explode the file into an array then perform a foreach loop on the array pieces, and return the $between[$i] values, so that they can be imploded back into an array and written to a file..

I can picture it in my head and i've exhausted every attempt, perhaps a fresh perspective could get me in the right direction... oh and btw, i've only been Ignited for 4 months, cut me some slack Smile
#2

[eluser]ηυмвєяσηє[/eluser]
Best way of doing this is using regular expressions.

If you need an example :
Code:
<?php
$html =file_get_contents('http://codeigniter.com/') ;
preg_match_all('/<a href="(.*)">(.*)<\/a>/Usi', $html, $pages);
echo '<pre>';
print_r($pages);
echo '</pre>';
#3

[eluser]ProImage Web[/eluser]
Thanks for the response! I've explored that also, but in the larger scheme of things, it won't work for what I'm trying to accomplish, I want to keep the code as versatile as possible so that almost any file could be parsed according to what's entered into the $start_string and $stop_string variables. If I purely wanted to do HTML files your solution would be perfect, and I do appreciate the response. Thanks again Smile
#4

[eluser]ηυмвєяσηє[/eluser]
Have you tried something like this ?
Code:
&lt;?php
$data = read_file($file_path.$filename);

$start_string = $this->input->post('start_string');
$stop_string = $this->input->post('stop_string');

preg_match_all("/" . $start_string. "(.*)" . $stop_string."/"  , $data, $strings);

echo '<pre>';
print_r($strings);
echo '</pre>'
#5

[eluser]ηυмвєяσηє[/eluser]
Okay this is my last example, tested by me.. ^^
Code:
$file_path = './';
$filename = 'mytext.txt';

$handle = fopen($file_path.$filename, "r");
$data = fread($handle, filesize($filename));

//$start_string = $this->input->post('start_string');
//$stop_string = $this->input->post('stop_string');
$start_string ='te';
$stop_string = 're';

preg_match_all('/' . $start_string. '(.*)' . $stop_string . '/Usi' , $data, $strings);

echo '<pre>';
print_r($strings[1]);
echo '</pre>';
#6

[eluser]ProImage Web[/eluser]
Finally got past the loop! Smile

Code:
function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'html|txt|js|css';;
        $config['max_size']    = '1024';
        $config['max_width']  = '1280';
        $config['max_height']  = '1024';
        $config['max_filename'] = '60';
        $config['remove_spaces'] = TRUE;

        $this->load->library('upload', $config);
        
        $this->_set_rules();

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->themes->view('home', $error);
            
        } else {
            
            $filename = $this->upload->file_name;
            $file_path = './uploads/';
            $write_path ='./uploads/data.php';
            $orig_name = $this->upload->orig_name;
            $start_string = $this->input->post('start_string');
            $stop_string = $this->input->post('stop_string');
            
            $data = read_file($file_path.$filename);
            $lines = explode("\n", $data);
            
            $m = 0;
            foreach ($lines as $line){
                
                $deduct = strlen($start_string);
                $m++;
                $between[$m] = substr(substr($line, strpos($line, $start_string), strpos($line, $stop_string) - strpos($line, $start_string)), $deduct)."\n";
                echo $between[$m];
            }
            
        }
        
        
        //$this->themes->view('results', $result);
        
    }
Now I have to figure out how to get that array of results formatted to input into a write_file() function.. any ideas?
#7

[eluser]ProImage Web[/eluser]
Finished Controller Function Smile

Code:
function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'css';;
        $config['max_size']    = '1024';
        $config['max_width']  = '1280';
        $config['max_height']  = '1024';
        $config['max_filename'] = '60';
        $config['remove_spaces'] = TRUE;

        $this->load->library('upload', $config);
        //validate
        $this->_set_rules();
        //show errors
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->themes->view('home', $error);
            
        } else {
            //do upload and parse the file
            $filename = $this->upload->file_name;
            $file_path = './uploads/';
            $write_path ='./uploads/data.php';
            $orig_name = $this->upload->orig_name;
            $start_string = $this->input->post('start_string');
            $stop_string = $this->input->post('stop_string');
            //read our file and place lines into an arry for reading
            $data1 = read_file($file_path.$filename);
            $lines = explode("\n", $data1);
            //for each line to read do something
            $newline = "\n";
            $between = "";
            foreach ($lines as $line){
                
                $deduct = strlen($start_string);
                $full = substr(substr($line, strpos($line, $start_string), strpos($line, $stop_string) - strpos($line, $start_string)), $deduct);
                //if something to output perform action else skip
                if ($full){
                    
                    $between .= $full.$newline;
                    write_file($write_path, $between, 'r+');
                    
                }
                
            }
            //delete our source file so not to clutter the server
            unlink($file_path.$filename);
        }
        //read the written file and output the data to the view
        $data['results'] = read_file($write_path);
        $this->themes->view('results', $data);
        
    }




Theme © iAndrew 2016 - Forum software by © MyBB